]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ia64/ia64/machdep.c
This commit was generated by cvs2svn to compensate for changes in r85756,
[FreeBSD/FreeBSD.git] / sys / ia64 / ia64 / machdep.c
1 /*-
2  * Copyright (c) 2000,2001 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include "opt_compat.h"
30 #include "opt_ddb.h"
31 #include "opt_ski.h"
32 #include "opt_msgbuf.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/eventhandler.h>
37 #include <sys/sysproto.h>
38 #include <sys/signalvar.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/lock.h>
42 #include <sys/pcpu.h>
43 #include <sys/malloc.h>
44 #include <sys/reboot.h>
45 #include <sys/bio.h>
46 #include <sys/buf.h>
47 #include <sys/mbuf.h>
48 #include <sys/vmmeter.h>
49 #include <sys/msgbuf.h>
50 #include <sys/exec.h>
51 #include <sys/sysctl.h>
52 #include <sys/uio.h>
53 #include <sys/linker.h>
54 #include <sys/random.h>
55 #include <sys/cons.h>
56 #include <net/netisr.h>
57 #include <vm/vm.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_map.h>
61 #include <vm/vm_extern.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_pager.h>
64 #include <sys/user.h>
65 #include <sys/ptrace.h>
66 #include <machine/clock.h>
67 #include <machine/md_var.h>
68 #include <machine/reg.h>
69 #include <machine/fpu.h>
70 #include <machine/pal.h>
71 #include <machine/sal.h>
72 #include <machine/bootinfo.h>
73 #include <machine/mutex.h>
74 #include <machine/vmparam.h>
75 #include <machine/elf.h>
76 #include <ddb/ddb.h>
77 #include <sys/vnode.h>
78 #include <machine/sigframe.h>
79 #include <machine/efi.h>
80 #include <machine/inst.h>
81 #include <machine/rse.h>
82 #include <machine/unwind.h>
83
84 #ifdef SKI
85 extern void ia64_ski_init(void);
86 #endif
87
88 u_int64_t processor_frequency;
89 u_int64_t bus_frequency;
90 u_int64_t itc_frequency;
91 int cold = 1;
92 struct bootinfo bootinfo;
93
94 struct mtx sched_lock;
95 struct mtx Giant;
96
97 extern char kstack[]; 
98 struct user *proc0uarea;
99 vm_offset_t proc0kstack;
100
101 extern u_int64_t kernel_text[], _end[];
102 extern u_int64_t _ia64_unwind_start[];
103 extern u_int64_t _ia64_unwind_end[];
104
105 u_int64_t ia64_port_base;
106
107 char machine[] = "ia64";
108 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
109
110 static char cpu_model[128];
111 SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD, cpu_model, 0, "");
112
113 #ifdef DDB
114 /* start and end of kernel symbol table */
115 void    *ksym_start, *ksym_end;
116 #endif
117
118 int     ia64_unaligned_print = 1;       /* warn about unaligned accesses */
119 int     ia64_unaligned_fix = 1; /* fix up unaligned accesses */
120 int     ia64_unaligned_sigbus = 0;      /* don't SIGBUS on fixed-up accesses */
121
122 SYSCTL_INT(_machdep, CPU_UNALIGNED_PRINT, unaligned_print,
123         CTLFLAG_RW, &ia64_unaligned_print, 0, "");
124
125 SYSCTL_INT(_machdep, CPU_UNALIGNED_FIX, unaligned_fix,
126         CTLFLAG_RW, &ia64_unaligned_fix, 0, "");
127
128 SYSCTL_INT(_machdep, CPU_UNALIGNED_SIGBUS, unaligned_sigbus,
129         CTLFLAG_RW, &ia64_unaligned_sigbus, 0, "");
130
131 static void cpu_startup __P((void *));
132 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
133
134 struct msgbuf *msgbufp=0;
135
136 int bootverbose = 0, Maxmem = 0;
137 long dumplo;
138
139 int     totalphysmem;           /* total amount of physical memory in system */
140 int     physmem;                /* physical memory used by NetBSD + some rsvd */
141 int     resvmem;                /* amount of memory reserved for PROM */
142 int     unusedmem;              /* amount of memory for OS that we don't use */
143 int     unknownmem;             /* amount of memory with an unknown use */
144 int     ncpus;                  /* number of cpus */
145
146 vm_offset_t phys_avail[10];
147
148 static int
149 sysctl_hw_physmem(SYSCTL_HANDLER_ARGS)
150 {
151         int error = sysctl_handle_int(oidp, 0, ia64_ptob(physmem), req);
152         return (error);
153 }
154
155 SYSCTL_PROC(_hw, HW_PHYSMEM, physmem, CTLTYPE_INT|CTLFLAG_RD,
156         0, 0, sysctl_hw_physmem, "I", "");
157
158 static int
159 sysctl_hw_usermem(SYSCTL_HANDLER_ARGS)
160 {
161         int error = sysctl_handle_int(oidp, 0,
162                 ia64_ptob(physmem - cnt.v_wire_count), req);
163         return (error);
164 }
165
166 SYSCTL_PROC(_hw, HW_USERMEM, usermem, CTLTYPE_INT|CTLFLAG_RD,
167         0, 0, sysctl_hw_usermem, "I", "");
168
169 SYSCTL_INT(_hw, OID_AUTO, availpages, CTLFLAG_RD, &physmem, 0, "");
170
171 /* must be 2 less so 0 0 can signal end of chunks */
172 #define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2)
173
174 static void identifycpu __P((void));
175
176 struct kva_md_info kmi;
177
178 static void
179 cpu_startup(dummy)
180         void *dummy;
181 {
182
183         /*
184          * Good {morning,afternoon,evening,night}.
185          */
186         identifycpu();
187
188         /* startrtclock(); */
189 #ifdef PERFMON
190         perfmon_init();
191 #endif
192         printf("real memory  = %ld (%ldK bytes)\n", ia64_ptob(Maxmem), ia64_ptob(Maxmem) / 1024);
193
194         /*
195          * Display any holes after the first chunk of extended memory.
196          */
197         if (bootverbose) {
198                 int indx;
199
200                 printf("Physical memory chunk(s):\n");
201                 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
202                         int size1 = phys_avail[indx + 1] - phys_avail[indx];
203
204                         printf("0x%08lx - 0x%08lx, %d bytes (%d pages)\n", phys_avail[indx],
205                             phys_avail[indx + 1] - 1, size1, size1 / PAGE_SIZE);
206                 }
207         }
208
209         vm_ksubmap_init(&kmi);
210
211 #if 0
212         /*
213          * Calculate callout wheel size
214          */
215         for (callwheelsize = 1, callwheelbits = 0;
216              callwheelsize < ncallout;
217              callwheelsize <<= 1, ++callwheelbits)
218                 ;
219         callwheelmask = callwheelsize - 1;
220
221         /*
222          * Allocate space for system data structures.
223          * The first available kernel virtual address is in "v".
224          * As pages of kernel virtual memory are allocated, "v" is incremented.
225          * As pages of memory are allocated and cleared,
226          * "firstaddr" is incremented.
227          * An index into the kernel page table corresponding to the
228          * virtual memory address maintained in "v" is kept in "mapaddr".
229          */
230
231         /*
232          * Make two passes.  The first pass calculates how much memory is
233          * needed and allocates it.  The second pass assigns virtual
234          * addresses to the various data structures.
235          */
236         firstaddr = 0;
237 again:
238         v = (caddr_t)firstaddr;
239
240 #define valloc(name, type, num) \
241             (name) = (type *)v; v = (caddr_t)((name)+(num))
242 #define valloclim(name, type, num, lim) \
243             (name) = (type *)v; v = (caddr_t)((lim) = ((name)+(num)))
244
245         valloc(callout, struct callout, ncallout);
246         valloc(callwheel, struct callout_tailq, callwheelsize);
247
248         /*
249          * Discount the physical memory larger than the size of kernel_map
250          * to avoid eating up all of KVA space.
251          */
252         if (kernel_map->first_free == NULL) {
253                 printf("Warning: no free entries in kernel_map.\n");
254                 physmem_est = physmem;
255         } else
256                 physmem_est = min(physmem, btoc(kernel_map->max_offset -
257                     kernel_map->min_offset));
258
259         /*
260          * The nominal buffer size (and minimum KVA allocation) is BKVASIZE.
261          * For the first 64MB of ram nominally allocate sufficient buffers to
262          * cover 1/4 of our ram.  Beyond the first 64MB allocate additional
263          * buffers to cover 1/20 of our ram over 64MB. When auto-sizing
264          * the buffer cache we limit the eventual kva reservation to
265          * maxbcache bytes.
266          */
267
268         if (nbuf == 0) {
269                 int factor = 4 * BKVASIZE / PAGE_SIZE;
270
271                 nbuf = 50;
272                 if (physmem_est > 1024)
273                         nbuf += min((physmem_est - 1024) / factor,
274                             16384 / factor);
275                 if (physmem_est > 16384)
276                         nbuf += (physmem_est - 16384) * 2 / (factor * 5);
277                 if (maxbcache && nbuf > maxbcache / BKVASIZE)
278                         nbuf = maxbcache / BKVASIZE;
279         }
280         nswbuf = max(min(nbuf/4, 64), 16);
281
282         valloc(swbuf, struct buf, nswbuf);
283         valloc(buf, struct buf, nbuf);
284         v = bufhashinit(v);
285
286         /*
287          * End of first pass, size has been calculated so allocate memory
288          */
289         if (firstaddr == 0) {
290                 size = (vm_size_t)(v - firstaddr);
291                 firstaddr = (vm_offset_t)kmem_alloc(kernel_map, round_page(size));
292                 if (firstaddr == 0)
293                         panic("startup: no room for tables");
294                 goto again;
295         }
296
297         /*
298          * End of second pass, addresses have been assigned
299          */
300         if ((vm_size_t)(v - firstaddr) != size)
301                 panic("startup: table size inconsistency");
302
303         clean_map = kmem_suballoc(kernel_map, &clean_sva, &clean_eva,
304                         (nbuf*BKVASIZE) + (nswbuf*MAXPHYS) + pager_map_size);
305         buffer_map = kmem_suballoc(clean_map, &buffer_sva, &buffer_eva,
306                                 (nbuf*BKVASIZE));
307         buffer_map->system_map = 1;
308         pager_map = kmem_suballoc(clean_map, &pager_sva, &pager_eva,
309                                 (nswbuf*MAXPHYS) + pager_map_size);
310         pager_map->system_map = 1;
311         exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
312                                 (16*(ARG_MAX+(PAGE_SIZE*3))));
313
314         /*
315          * Finally, allocate mbuf pool.
316          * XXX: Mbuf system machine-specific initializations should
317          *      go here, if anywhere.
318          */
319
320         /*
321          * Initialize callouts
322          */
323         SLIST_INIT(&callfree);
324         for (i = 0; i < ncallout; i++) {
325                 callout_init(&callout[i], 0);
326                 callout[i].c_flags = CALLOUT_LOCAL_ALLOC;
327                 SLIST_INSERT_HEAD(&callfree, &callout[i], c_links.sle);
328         }
329
330         for (i = 0; i < callwheelsize; i++) {
331                 TAILQ_INIT(&callwheel[i]);
332         }
333
334         mtx_init(&callout_lock, "callout", MTX_SPIN | MTX_RECURSE);
335 #endif
336
337 #if defined(USERCONFIG)
338 #if defined(USERCONFIG_BOOT)
339         if (1)
340 #else
341         if (boothowto & RB_CONFIG)
342 #endif
343         {
344                 userconfig();
345                 cninit();       /* the preferred console may have changed */
346         }
347 #endif
348
349         printf("avail memory = %ld (%ldK bytes)\n", ptoa(cnt.v_free_count),
350             ptoa(cnt.v_free_count) / 1024);
351
352         /*
353          * Set up buffers, so they can be used to read disk labels.
354          */
355         bufinit();
356         vm_pager_bufferinit();
357 }
358
359 static void
360 identifycpu(void)
361 {
362         char vendor[17];
363         u_int64_t t;
364         int number, revision, model, family, archrev;
365         u_int64_t features;
366
367         /*
368          * Assumes little-endian.
369          */
370         *(u_int64_t *) &vendor[0] = ia64_get_cpuid(0);
371         *(u_int64_t *) &vendor[8] = ia64_get_cpuid(1);
372         vendor[16] = '\0';
373
374         t = ia64_get_cpuid(3);
375         number = (t >> 0) & 0xff;
376         revision = (t >> 8) & 0xff;
377         model = (t >> 16) & 0xff;
378         family = (t >> 24) & 0xff;
379         archrev = (t >> 32) & 0xff;
380
381         if (family == 0x7)
382                 strcpy(cpu_model, "Itanium");
383         else if (family == 0x1f)
384                 strcpy(cpu_model, "McKinley");
385         else
386                 snprintf(cpu_model, sizeof(cpu_model), "Family=%d", family);
387
388         features = ia64_get_cpuid(4);
389
390         printf("CPU: %s", cpu_model);
391         if (processor_frequency)
392                 printf(" (%ld.%02ld-Mhz)\n",
393                        (processor_frequency + 4999) / 1000000,
394                        ((processor_frequency + 4999) / 10000) % 100);
395         else
396                 printf("\n");
397         printf("  Origin = \"%s\"  Model = %d  Revision = %d\n",
398                vendor, model, revision);
399         printf("  Features = 0x%b\n", (u_int32_t) features,
400                "\020"
401                "\001LB");
402 }
403
404 static void
405 add_kernel_unwind_tables(void *arg)
406 {
407         /*
408          * Register the kernel's unwind table.
409          */
410         ia64_add_unwind_table(kernel_text,
411                               _ia64_unwind_start,
412                               _ia64_unwind_end);
413 }
414 SYSINIT(unwind, SI_SUB_KMEM, SI_ORDER_ANY, add_kernel_unwind_tables, 0);
415
416 static void
417 map_pal_code(void)
418 {
419         EFI_MEMORY_DESCRIPTOR *md, *mdp;
420         int mdcount, i;
421         u_int64_t psr;
422         struct ia64_pte pte;
423         vm_offset_t addr;
424
425         if (!bootinfo.bi_systab)
426                 return;
427
428         mdcount = bootinfo.bi_memmap_size / bootinfo.bi_memdesc_size;
429         md = (EFI_MEMORY_DESCRIPTOR *) IA64_PHYS_TO_RR7(bootinfo.bi_memmap);
430
431         for (i = 0, mdp = md; i < mdcount; i++,
432                  mdp = NextMemoryDescriptor(mdp, bootinfo.bi_memdesc_size)) {
433                 if (mdp->Type == EfiPalCode)
434                         break;
435         }
436
437         if (i == mdcount) {
438                 printf("Can't find PAL Code\n");
439                 return;
440         }
441
442         /*
443          * We use a TR to map the first 256M of memory - this might
444          * cover the palcode too.
445          */
446         if ((mdp->PhysicalStart & ~((1 << 28) - 1)) == 0) {
447                 printf("PAL Code is mapped by the kernel's TR\n");
448                 return;
449         }
450
451         addr = mdp->PhysicalStart & ~((1 << 28) - 1);
452
453         bzero(&pte, sizeof(pte));
454         pte.pte_p = 1;
455         pte.pte_ma = PTE_MA_WB;
456         pte.pte_a = 1;
457         pte.pte_d = 1;
458         pte.pte_pl = PTE_PL_KERN;
459         pte.pte_ar = PTE_AR_RWX;
460         pte.pte_ppn = addr >> 12;
461
462         __asm __volatile("mov %0=psr;;" : "=r" (psr));
463         __asm __volatile("rsm psr.ic|psr.i;; srlz.i;;");
464         __asm __volatile("mov cr.ifa=%0" :: "r"(IA64_PHYS_TO_RR7(addr)));
465         __asm __volatile("mov cr.itir=%0" :: "r"(28 << 2));
466         __asm __volatile("srlz.i;;");
467         __asm __volatile("itr.i itr[%0]=%1;;"
468                          :: "r"(2), "r"(*(u_int64_t*)&pte));
469         __asm __volatile("srlz.i;;");
470         __asm __volatile("mov psr.l=%0;; srlz.i;;" :: "r" (psr));
471 }
472
473 static void
474 calculate_frequencies(void)
475 {
476         struct ia64_sal_result sal;
477         struct ia64_pal_result pal;
478
479         sal = ia64_sal_entry(SAL_FREQ_BASE, 0, 0, 0, 0, 0, 0, 0);
480         pal = ia64_call_pal_static(PAL_FREQ_RATIOS, 0, 0, 0);
481
482         if (sal.sal_status == 0 && pal.pal_status == 0) {
483                 if (bootverbose) {
484                         printf("Platform clock frequency %ld Hz\n",
485                                sal.sal_result[0]);
486                         printf("Processor ratio %ld/%ld, Bus ratio %ld/%ld, "
487                                "ITC ratio %ld/%ld\n",
488                                pal.pal_result[0] >> 32,
489                                pal.pal_result[0] & ((1L << 32) - 1),
490                                pal.pal_result[1] >> 32,
491                                pal.pal_result[1] & ((1L << 32) - 1),
492                                pal.pal_result[2] >> 32,
493                                pal.pal_result[2] & ((1L << 32) - 1));
494                 }
495                 processor_frequency =
496                         sal.sal_result[0] * (pal.pal_result[0] >> 32)
497                         / (pal.pal_result[0] & ((1L << 32) - 1));
498                 bus_frequency =
499                         sal.sal_result[0] * (pal.pal_result[1] >> 32)
500                         / (pal.pal_result[1] & ((1L << 32) - 1));
501                 itc_frequency =
502                         sal.sal_result[0] * (pal.pal_result[2] >> 32)
503                         / (pal.pal_result[2] & ((1L << 32) - 1));
504         }
505 }
506
507 void
508 ia64_init(u_int64_t arg1, u_int64_t arg2)
509 {
510         int phys_avail_cnt;
511         vm_offset_t kernstart, kernend;
512         vm_offset_t kernstartpfn, kernendpfn, pfn0, pfn1;
513         char *p;
514         EFI_MEMORY_DESCRIPTOR *md, *mdp;
515         int mdcount, i;
516
517         /* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
518
519         /*
520          * TODO: Disable interrupts, floating point etc.
521          * Maybe flush cache and tlb
522          */
523         ia64_set_fpsr(IA64_FPSR_DEFAULT);
524
525         /*
526          * TODO: Get critical system information (if possible, from the
527          * information provided by the boot program).
528          */
529
530         /*
531          * Gross and disgusting hack. The bootinfo is written into
532          * memory at a fixed address.
533          */
534         bootinfo = *(struct bootinfo *) 0xe000000000508000;
535         if (bootinfo.bi_magic != BOOTINFO_MAGIC
536             || bootinfo.bi_version != 1) {
537                 bzero(&bootinfo, sizeof(bootinfo));
538                 bootinfo.bi_kernend = (vm_offset_t) round_page(_end);
539         }
540
541         /*
542          * Look for the I/O ports first - we need them for console
543          * probing.
544          */
545         mdcount = bootinfo.bi_memmap_size / bootinfo.bi_memdesc_size;
546         md = (EFI_MEMORY_DESCRIPTOR *) IA64_PHYS_TO_RR7(bootinfo.bi_memmap);
547         if (md == NULL || mdcount == 0) {
548 #ifdef SKI
549                 static EFI_MEMORY_DESCRIPTOR ski_md[2];
550                 /*
551                  * XXX hack for ski. In reality, the loader will probably ask
552                  * EFI and pass the results to us. Possibly, we will call EFI
553                  * directly.
554                  */
555                 ski_md[0].Type = EfiConventionalMemory;
556                 ski_md[0].PhysicalStart = 2L*1024*1024;
557                 ski_md[0].VirtualStart = 0;
558                 ski_md[0].NumberOfPages = (64L*1024*1024)>>12;
559                 ski_md[0].Attribute = EFI_MEMORY_WB;
560
561                 ski_md[1].Type = EfiMemoryMappedIOPortSpace;
562                 ski_md[1].PhysicalStart = 0xffffc000000;
563                 ski_md[1].VirtualStart = 0;
564                 ski_md[1].NumberOfPages = (64L*1024*1024)>>12;
565                 ski_md[1].Attribute = EFI_MEMORY_UC;
566         
567                 md = ski_md;
568                 mdcount = 2;
569 #endif
570         }
571
572         for (i = 0, mdp = md; i < mdcount; i++,
573                  mdp = NextMemoryDescriptor(mdp, bootinfo.bi_memdesc_size)) {
574                 if (mdp->Type == EfiMemoryMappedIOPortSpace) {
575                         ia64_port_base = IA64_PHYS_TO_RR6(mdp->PhysicalStart);
576                 }
577         }
578
579         /*
580          * Look at arguments passed to us and compute boothowto.
581          */
582         boothowto = bootinfo.bi_boothowto;
583 #ifdef KADB
584         boothowto |= RB_KDB;
585 #endif
586
587         /*
588          * Catch case of boot_verbose set in environment.
589          */
590         if ((p = getenv("boot_verbose")) != NULL) {
591                 if (strcmp(p, "yes") == 0 || strcmp(p, "YES") == 0) {
592                         boothowto |= RB_VERBOSE;
593                 }
594         }
595
596         if (boothowto & RB_VERBOSE)
597                 bootverbose = 1;
598
599         /*
600          * Initialize the console before we print anything out.
601          */
602         cninit();
603  
604         /* OUTPUT NOW ALLOWED */
605
606         /*
607          * Wire things up so we can call the firmware.
608          */
609         map_pal_code();
610         ia64_efi_init();
611 #ifdef SKI
612         ia64_ski_init();
613 #endif
614         calculate_frequencies();
615
616         /*
617          * Find the beginning and end of the kernel.
618          */
619         kernstart = trunc_page(kernel_text);
620         ksym_start = (void *)bootinfo.bi_symtab;
621         ksym_end   = (void *)bootinfo.bi_esymtab;
622         kernend = (vm_offset_t)round_page(ksym_end);
623         /* But if the bootstrap tells us otherwise, believe it! */
624         if (bootinfo.bi_kernend)
625                 kernend = round_page(bootinfo.bi_kernend);
626         preload_metadata = (caddr_t)bootinfo.bi_modulep;
627         if (envmode == 1)
628                 kern_envp = static_env;
629         else
630                 kern_envp = (caddr_t)bootinfo.bi_envp;
631
632         /* Init basic tunables, including hz */
633         init_param();
634
635         p = getenv("kernelname");
636         if (p)
637                 strncpy(kernelname, p, sizeof(kernelname) - 1);
638
639         kernstartpfn = atop(IA64_RR_MASK(kernstart));
640         kernendpfn = atop(IA64_RR_MASK(kernend));
641
642         /*
643          * Size the memory regions and load phys_avail[] with the results.
644          */
645
646         /*
647          * Find out how much memory is available, by looking at
648          * the memory descriptors.
649          */
650
651 #ifdef DEBUG_MD
652         printf("Memory descriptor count: %d\n", mdcount);
653 #endif
654
655         phys_avail_cnt = 0;
656         for (i = 0, mdp = md; i < mdcount; i++,
657                  mdp = NextMemoryDescriptor(mdp, bootinfo.bi_memdesc_size)) {
658 #ifdef DEBUG_MD
659                 printf("MD %d: type %d pa 0x%lx cnt 0x%lx\n", i,
660                        mdp->Type,
661                        mdp->PhysicalStart,
662                        mdp->NumberOfPages);
663 #endif
664
665                 pfn0 = ia64_btop(round_page(mdp->PhysicalStart));
666                 pfn1 = ia64_btop(trunc_page(mdp->PhysicalStart
667                                             + mdp->NumberOfPages * 4096));
668                 if (pfn1 <= pfn0)
669                         continue;
670
671                 if (mdp->Type != EfiConventionalMemory) {
672                         resvmem += (pfn1 - pfn0);
673                         continue;
674                 }
675
676                 totalphysmem += (pfn1 - pfn0);
677
678                 /*
679                  * We have a memory descriptors available for system
680                  * software use.  We must determine if this cluster
681                  * holds the kernel.
682                  */
683                 physmem += (pfn1 - pfn0);
684                 if (pfn0 <= kernendpfn && kernstartpfn <= pfn1) {
685                         /*
686                          * Must compute the location of the kernel
687                          * within the segment.
688                          */
689 #ifdef DEBUG_MD
690                         printf("Descriptor %d contains kernel\n", i);
691 #endif
692                         if (pfn0 < kernstartpfn) {
693                                 /*
694                                  * There is a chunk before the kernel.
695                                  */
696 #ifdef DEBUG_MD
697                                 printf("Loading chunk before kernel: "
698                                        "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
699 #endif
700                                 phys_avail[phys_avail_cnt] = ia64_ptob(pfn0);
701                                 phys_avail[phys_avail_cnt+1] = ia64_ptob(kernstartpfn);
702                                 phys_avail_cnt += 2;
703                         }
704                         if (kernendpfn < pfn1) {
705                                 /*
706                                  * There is a chunk after the kernel.
707                                  */
708 #ifdef DEBUG_MD
709                                 printf("Loading chunk after kernel: "
710                                        "0x%lx / 0x%lx\n", kernendpfn, pfn1);
711 #endif
712                                 phys_avail[phys_avail_cnt] = ia64_ptob(kernendpfn);
713                                 phys_avail[phys_avail_cnt+1] = ia64_ptob(pfn1);
714                                 phys_avail_cnt += 2;
715                         }
716                 } else {
717                         /*
718                          * Just load this cluster as one chunk.
719                          */
720 #ifdef DEBUG_MD
721                         printf("Loading descriptor %d: 0x%lx / 0x%lx\n", i,
722                                pfn0, pfn1);
723 #endif
724                         phys_avail[phys_avail_cnt] = ia64_ptob(pfn0);
725                         phys_avail[phys_avail_cnt+1] = ia64_ptob(pfn1);
726                         phys_avail_cnt += 2;
727                         
728                 }
729         }
730         phys_avail[phys_avail_cnt] = 0;
731
732         Maxmem = physmem;
733
734         /*
735          * Initialize error message buffer (at end of core).
736          */
737         {
738                 size_t sz = round_page(MSGBUF_SIZE);
739                 int i = phys_avail_cnt - 2;
740
741                 /* shrink so that it'll fit in the last segment */
742                 if (phys_avail[i+1] - phys_avail[i] < sz)
743                         sz = phys_avail[i+1] - phys_avail[i];
744
745                 phys_avail[i+1] -= sz;
746                 msgbufp = (struct msgbuf*) IA64_PHYS_TO_RR7(phys_avail[i+1]);
747
748                 msgbufinit(msgbufp, sz);
749
750                 /* Remove the last segment if it now has no pages. */
751                 if (phys_avail[i] == phys_avail[i+1])
752                         phys_avail[i] = 0;
753
754                 /* warn if the message buffer had to be shrunk */
755                 if (sz != round_page(MSGBUF_SIZE))
756                         printf("WARNING: %ld bytes not available for msgbuf in last cluster (%ld used)\n",
757                             round_page(MSGBUF_SIZE), sz);
758
759         }
760
761         proc_linkup(&proc0);
762         /*
763          * Init mapping for u page(s) for proc 0
764          */
765         proc0uarea = (struct user *)pmap_steal_memory(UAREA_PAGES * PAGE_SIZE);
766         proc0kstack = (vm_offset_t)kstack;
767         proc0.p_uarea = proc0uarea;
768         thread0 = &proc0.p_thread;
769         thread0->td_kstack = proc0kstack;
770         thread0->td_pcb = (struct pcb *)
771             (thread0->td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
772         /*
773          * Setup the global data for the bootstrap cpu.
774          */
775         {
776                 /* This is not a 'struct user' */
777                 size_t sz = round_page(KSTACK_PAGES * PAGE_SIZE);
778                 globalp = (struct globaldata *) pmap_steal_memory(sz);
779                 globaldata_init(globalp, 0, sz);
780                 ia64_set_k4((u_int64_t) globalp);
781                 PCPU_GET(next_asn) = 1; /* 0 used for proc0 pmap */
782         }
783
784         /*
785          * Initialize the virtual memory system.
786          */
787         pmap_bootstrap();
788
789         /*
790          * Initialize the rest of proc 0's PCB.
791          *
792          * Set the kernel sp, reserving space for an (empty) trapframe,
793          * and make proc0's trapframe pointer point to it for sanity.
794          * Initialise proc0's backing store to start after u area.
795          *
796          * XXX what is all this +/- 16 stuff?
797          */
798         thread0->td_frame = (struct trapframe *)thread0->td_pcb - 1;
799         thread0->td_pcb->pcb_sp = (u_int64_t)thread0->td_frame - 16;
800         thread0->td_pcb->pcb_bspstore = (u_int64_t)proc0kstack;
801
802         /* Setup curproc so that mutexes work */
803         PCPU_SET(curthread, thread0);
804         PCPU_SET(spinlocks, NULL);
805
806         LIST_INIT(&thread0->td_contested);
807
808         /*
809          * Initialise mutexes.
810          */
811         mtx_init(&Giant, "Giant", MTX_DEF | MTX_RECURSE);
812         mtx_init(&sched_lock, "sched lock", MTX_SPIN | MTX_RECURSE);
813         mtx_init(&proc0.p_mtx, "process lock", MTX_DEF);
814         mtx_lock(&Giant);
815
816         /*
817          * Initialize debuggers, and break into them if appropriate.
818          */
819 #ifdef DDB
820         kdb_init();
821         if (boothowto & RB_KDB) {
822                 printf("Boot flags requested debugger\n");
823                 breakpoint();
824         }
825 #endif
826 }
827
828 int
829 ia64_running_in_simulator()
830 {
831         return bootinfo.bi_systab == 0;
832 }
833
834 void
835 bzero(void *buf, size_t len)
836 {
837         caddr_t p = buf;
838
839         while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
840                 *p++ = 0;
841                 len--;
842         }
843         while (len >= sizeof(u_long) * 8) {
844                 *(u_long*) p = 0;
845                 *((u_long*) p + 1) = 0;
846                 *((u_long*) p + 2) = 0;
847                 *((u_long*) p + 3) = 0;
848                 len -= sizeof(u_long) * 8;
849                 *((u_long*) p + 4) = 0;
850                 *((u_long*) p + 5) = 0;
851                 *((u_long*) p + 6) = 0;
852                 *((u_long*) p + 7) = 0;
853                 p += sizeof(u_long) * 8;
854         }
855         while (len >= sizeof(u_long)) {
856                 *(u_long*) p = 0;
857                 len -= sizeof(u_long);
858                 p += sizeof(u_long);
859         }
860         while (len) {
861                 *p++ = 0;
862                 len--;
863         }
864 }
865
866 void
867 DELAY(int n)
868 {
869         u_int64_t start, end, now;
870
871         /*
872          * XXX This can't cope with rollovers.
873          */
874         start = ia64_get_itc();
875         end = start + (itc_frequency * n) / 1000000;
876         /* printf("DELAY from 0x%lx to 0x%lx\n", start, end); */
877         do {
878                 now = ia64_get_itc();
879         } while (now < end);
880 }
881
882 /*
883  * Send an interrupt to process.
884  *
885  * Stack is set up to allow sigcode stored
886  * at top to call routine, followed by kcall
887  * to sigreturn routine below.  After sigreturn
888  * resets the signal mask, the stack, and the
889  * frame pointer, it returns to the user
890  * specified pc, psl.
891  */
892 void
893 sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
894 {
895         struct proc *p;
896         struct thread *td;
897         struct trapframe *frame;
898         struct sigacts *psp;
899         struct sigframe sf, *sfp;
900         u_int64_t sbs = 0;
901         int oonstack, rndfsize;
902
903         td = curthread;
904         p = td->td_proc;
905         PROC_LOCK_ASSERT(p, MA_OWNED);
906         psp = p->p_sigacts;
907         frame = td->td_frame;
908         oonstack = sigonstack(frame->tf_r[FRAME_SP]);
909         rndfsize = ((sizeof(sf) + 15) / 16) * 16;
910
911         /*
912          * Make sure that we restore the entire trapframe after a
913          * signal.
914          */
915         frame->tf_flags &= ~FRAME_SYSCALL;
916
917         /* save user context */
918         bzero(&sf, sizeof(struct sigframe));
919         sf.sf_uc.uc_sigmask = *mask;
920         sf.sf_uc.uc_stack = p->p_sigstk;
921         sf.sf_uc.uc_stack.ss_flags = (p->p_flag & P_ALTSTACK)
922             ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
923         sf.sf_uc.uc_mcontext.mc_flags = IA64_MC_FLAG_ONSTACK;
924         sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
925
926         sf.sf_uc.uc_mcontext.mc_nat     = 0; /* XXX */
927         sf.sf_uc.uc_mcontext.mc_sp      = frame->tf_r[FRAME_SP];
928         sf.sf_uc.uc_mcontext.mc_ip      = (frame->tf_cr_iip
929                                            | ((frame->tf_cr_ipsr >> 41) & 3));
930         sf.sf_uc.uc_mcontext.mc_cfm     = frame->tf_cr_ifs & ~(1<<31);
931         sf.sf_uc.uc_mcontext.mc_um      = frame->tf_cr_ipsr & 0x1fff;
932         sf.sf_uc.uc_mcontext.mc_ar_rsc  = frame->tf_ar_rsc;
933         sf.sf_uc.uc_mcontext.mc_ar_bsp  = frame->tf_ar_bspstore;
934         sf.sf_uc.uc_mcontext.mc_ar_rnat = frame->tf_ar_rnat;
935         sf.sf_uc.uc_mcontext.mc_ar_ccv  = frame->tf_ar_ccv;
936         sf.sf_uc.uc_mcontext.mc_ar_unat = frame->tf_ar_unat;
937         sf.sf_uc.uc_mcontext.mc_ar_fpsr = frame->tf_ar_fpsr;
938         sf.sf_uc.uc_mcontext.mc_ar_pfs  = frame->tf_ar_pfs;
939         sf.sf_uc.uc_mcontext.mc_pr      = frame->tf_pr;
940
941         bcopy(&frame->tf_b[0],
942               &sf.sf_uc.uc_mcontext.mc_br[0],
943               8 * sizeof(unsigned long));
944         sf.sf_uc.uc_mcontext.mc_gr[0] = 0;
945         bcopy(&frame->tf_r[0],
946               &sf.sf_uc.uc_mcontext.mc_gr[1],
947               31 * sizeof(unsigned long));
948
949         /* XXX mc_fr[] */
950
951         /*
952          * Allocate and validate space for the signal handler
953          * context. Note that if the stack is in P0 space, the
954          * call to grow() is a nop, and the useracc() check
955          * will fail if the process has not already allocated
956          * the space with a `brk'.
957          */
958         if ((p->p_flag & P_ALTSTACK) != 0 && !oonstack &&
959             SIGISMEMBER(psp->ps_sigonstack, sig)) {
960                 sbs = (u_int64_t) p->p_sigstk.ss_sp;
961                 sfp = (struct sigframe *)((caddr_t)p->p_sigstk.ss_sp +
962                     p->p_sigstk.ss_size - rndfsize);
963                 /*
964                  * Align sp and bsp.
965                  */
966                 sbs = (sbs + 15) & ~15;
967                 sfp = (struct sigframe *)((u_int64_t)sfp & ~15);
968 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
969                 p->p_sigstk.ss_flags |= SS_ONSTACK;
970 #endif
971         } else
972                 sfp = (struct sigframe *)(frame->tf_r[FRAME_SP] - rndfsize);
973         PROC_UNLOCK(p);
974
975         (void)grow_stack(p, (u_long)sfp);
976 #ifdef DEBUG
977         if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
978                 printf("sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
979                        sig, &sf, sfp);
980 #endif
981         if (!useracc((caddr_t)sfp, sizeof(sf), VM_PROT_WRITE)) {
982 #ifdef DEBUG
983                 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
984                         printf("sendsig(%d): useracc failed on sig %d\n",
985                                p->p_pid, sig);
986 #endif
987                 /*
988                  * Process has trashed its stack; give it an illegal
989                  * instruction to halt it in its tracks.
990                  */
991                 PROC_LOCK(p);
992                 SIGACTION(p, SIGILL) = SIG_DFL;
993                 SIGDELSET(p->p_sigignore, SIGILL);
994                 SIGDELSET(p->p_sigcatch, SIGILL);
995                 SIGDELSET(p->p_sigmask, SIGILL);
996                 psignal(p, SIGILL);
997                 return;
998         }
999
1000 #if 0
1001         /* save the floating-point state, if necessary, then copy it. */
1002         ia64_fpstate_save(td, 1);
1003         sf.sf_uc.uc_mcontext.mc_ownedfp = td->td_md.md_flags & MDP_FPUSED;
1004         bcopy(&td->td_pcb->pcb_fp,
1005               (struct fpreg *)sf.sf_uc.uc_mcontext.mc_fpregs,
1006               sizeof(struct fpreg));
1007         sf.sf_uc.uc_mcontext.mc_fp_control = td->td_pcb.pcb_fp_control;
1008 #endif
1009
1010         /*
1011          * copy the frame out to userland.
1012          */
1013         (void) copyout((caddr_t)&sf, (caddr_t)sfp, sizeof(sf));
1014 #ifdef DEBUG
1015         if (sigdebug & SDB_FOLLOW)
1016                 printf("sendsig(%d): sig %d sfp %p code %lx\n", p->p_pid, sig,
1017                     sfp, code);
1018 #endif
1019
1020         /*
1021          * Set up the registers to return to sigcode.
1022          */
1023         frame->tf_cr_ipsr &= ~IA64_PSR_RI;
1024         frame->tf_cr_iip = PS_STRINGS - (esigcode - sigcode);
1025         frame->tf_r[FRAME_R1] = sig;
1026         PROC_LOCK(p);
1027         if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) {
1028                 frame->tf_r[FRAME_R15] = (u_int64_t)&(sfp->sf_si);
1029
1030                 /* Fill in POSIX parts */
1031                 sf.sf_si.si_signo = sig;
1032                 sf.sf_si.si_code = code;
1033                 sf.sf_si.si_addr = (void*)frame->tf_cr_ifa;
1034         }
1035         else
1036                 frame->tf_r[FRAME_R15] = code;
1037
1038         frame->tf_r[FRAME_SP] = (u_int64_t)sfp - 16;
1039         frame->tf_r[FRAME_R14] = sig;
1040         frame->tf_r[FRAME_R15] = (u_int64_t) &sfp->sf_si;
1041         frame->tf_r[FRAME_R16] = (u_int64_t) &sfp->sf_uc;
1042         frame->tf_r[FRAME_R17] = (u_int64_t)catcher;
1043         frame->tf_r[FRAME_R18] = sbs;
1044
1045 #ifdef DEBUG
1046         if (sigdebug & SDB_FOLLOW)
1047                 printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
1048                     frame->tf_cr_iip, frame->tf_regs[FRAME_R4]);
1049         if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1050                 printf("sendsig(%d): sig %d returns\n",
1051                     p->p_pid, sig);
1052 #endif
1053 }
1054
1055 /*
1056  * System call to cleanup state after a signal
1057  * has been taken.  Reset signal mask and
1058  * stack state from context left by sendsig (above).
1059  * Return to previous pc and psl as specified by
1060  * context left by sendsig. Check carefully to
1061  * make sure that the user has not modified the
1062  * state to gain improper privileges.
1063  */
1064 #ifdef COMPAT_43
1065 int
1066 osigreturn(struct thread *td,
1067         struct osigreturn_args /* {
1068                 struct osigcontext *sigcntxp;
1069         } */ *uap)
1070 {
1071         return EOPNOTSUPP;
1072 }
1073 #endif
1074
1075 /*
1076  * System call to cleanup state after a signal
1077  * has been taken.  Reset signal mask and
1078  * stack state from context left by sendsig (above).
1079  * Return to previous pc and psl as specified by
1080  * context left by sendsig. Check carefully to
1081  * make sure that the user has not modified the
1082  * state to gain improper privileges.
1083  */
1084
1085 int
1086 sigreturn(struct thread *td,
1087         struct sigreturn_args /* {
1088                 ucontext_t *sigcntxp;
1089         } */ *uap)
1090 {
1091         ucontext_t uc, *ucp;
1092         struct pcb *pcb;
1093         struct trapframe *frame = td->td_frame;
1094         struct __mcontext *mcp;
1095         struct proc *p;
1096
1097         ucp = uap->sigcntxp;
1098         pcb = td->td_pcb;
1099         p = td->td_proc;
1100
1101 #ifdef DEBUG
1102         if (sigdebug & SDB_FOLLOW)
1103             printf("sigreturn: pid %d, scp %p\n", p->p_pid, ucp);
1104 #endif
1105
1106         /*
1107          * Fetch the entire context structure at once for speed.
1108          * We don't use a normal argument to simplify RSE handling.
1109          */
1110         if (copyin((caddr_t)frame->tf_r[FRAME_R4],
1111                    (caddr_t)&uc, sizeof(ucontext_t)))
1112                 return (EFAULT);
1113
1114         if (frame->tf_ndirty != 0) {
1115             printf("sigreturn: dirty user stacked registers\n");
1116         }
1117
1118         /*
1119          * Restore the user-supplied information
1120          */
1121         mcp = &uc.uc_mcontext;
1122         bcopy(&mcp->mc_br[0], &frame->tf_b[0], 8*sizeof(u_int64_t));
1123         bcopy(&mcp->mc_gr[1], &frame->tf_r[0], 31*sizeof(u_int64_t));
1124         /* XXX mc_fr */
1125
1126         frame->tf_flags &= ~FRAME_SYSCALL;
1127         frame->tf_cr_iip = mcp->mc_ip & ~15;
1128         frame->tf_cr_ipsr &= ~IA64_PSR_RI;
1129         switch (mcp->mc_ip & 15) {
1130         case 1:
1131                 frame->tf_cr_ipsr |= IA64_PSR_RI_1;
1132                 break;
1133         case 2:
1134                 frame->tf_cr_ipsr |= IA64_PSR_RI_2;
1135                 break;
1136         }
1137         frame->tf_cr_ipsr     = ((frame->tf_cr_ipsr & ~0x1fff)
1138                                  | (mcp->mc_um & 0x1fff));
1139         frame->tf_pr          = mcp->mc_pr;
1140         frame->tf_ar_rsc      = (mcp->mc_ar_rsc & 3) | 12; /* user, loadrs=0 */
1141         frame->tf_ar_pfs      = mcp->mc_ar_pfs;
1142         frame->tf_cr_ifs      = mcp->mc_cfm | (1UL<<63);
1143         frame->tf_ar_bspstore = mcp->mc_ar_bsp;
1144         frame->tf_ar_rnat     = mcp->mc_ar_rnat;
1145         frame->tf_ndirty      = 0; /* assumes flushrs in sigcode */
1146         frame->tf_ar_unat     = mcp->mc_ar_unat;
1147         frame->tf_ar_ccv      = mcp->mc_ar_ccv;
1148         frame->tf_ar_fpsr     = mcp->mc_ar_fpsr;
1149
1150         frame->tf_r[FRAME_SP] = mcp->mc_sp;
1151
1152         PROC_LOCK(p);
1153 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1154         if (uc.uc_mcontext.mc_onstack & 1)
1155                 p->p_sigstk.ss_flags |= SS_ONSTACK;
1156         else
1157                 p->p_sigstk.ss_flags &= ~SS_ONSTACK;
1158 #endif
1159
1160         p->p_sigmask = uc.uc_sigmask;
1161         SIG_CANTMASK(p->p_sigmask);
1162         PROC_UNLOCK(p);
1163
1164         /* XXX ksc.sc_ownedfp ? */
1165         ia64_fpstate_drop(td);
1166 #if 0
1167         bcopy((struct fpreg *)uc.uc_mcontext.mc_fpregs,
1168               &td->td_pcb->pcb_fp, sizeof(struct fpreg));
1169         td->td_pcb->pcb_fp_control = uc.uc_mcontext.mc_fp_control;
1170 #endif
1171
1172 #ifdef DEBUG
1173         if (sigdebug & SDB_FOLLOW)
1174                 printf("sigreturn(%d): returns\n", p->p_pid);
1175 #endif
1176         return (EJUSTRETURN);
1177 }
1178
1179 /*
1180  * Machine dependent boot() routine
1181  */
1182 void
1183 cpu_boot(int howto)
1184 {
1185
1186         ia64_efi_runtime->ResetSystem(EfiResetWarm, EFI_SUCCESS, 0, 0);
1187 }
1188
1189 /*
1190  * Shutdown the CPU as much as possible
1191  */
1192 void
1193 cpu_halt(void)
1194 {
1195
1196         ia64_efi_runtime->ResetSystem(EfiResetWarm, EFI_SUCCESS, 0, 0);
1197 }
1198
1199 /*
1200  * Clear registers on exec
1201  */
1202 void
1203 setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
1204 {
1205         struct trapframe *frame;
1206
1207         frame = td->td_frame;
1208
1209         /*
1210          * Make sure that we restore the entire trapframe after an
1211          * execve.
1212          */
1213         frame->tf_flags &= ~FRAME_SYSCALL;
1214
1215         bzero(frame->tf_r, sizeof(frame->tf_r));
1216         bzero(frame->tf_f, sizeof(frame->tf_f));
1217         frame->tf_cr_iip = entry;
1218         frame->tf_cr_ipsr = (IA64_PSR_IC
1219                              | IA64_PSR_I
1220                              | IA64_PSR_IT
1221                              | IA64_PSR_DT
1222                              | IA64_PSR_RT
1223                              | IA64_PSR_DFH
1224                              | IA64_PSR_BN
1225                              | IA64_PSR_CPL_USER);
1226         /*
1227          * Make sure that sp is aligned to a 16 byte boundary and
1228          * reserve 16 bytes of scratch space for _start.
1229          */
1230         frame->tf_r[FRAME_SP] = (stack & ~15) - 16;
1231
1232         /*
1233          * Write values for out0, out1 and out2 to the user's backing
1234          * store and arrange for them to be restored into the user's
1235          * initial register frame. Assumes that (bspstore & 0x1f8) <
1236          * 0x1e0.
1237          */
1238         frame->tf_ar_bspstore = td->td_md.md_bspstore + 24;
1239         suword((caddr_t) frame->tf_ar_bspstore - 24, stack);
1240         suword((caddr_t) frame->tf_ar_bspstore - 16, ps_strings);
1241         suword((caddr_t) frame->tf_ar_bspstore -  8, 0);
1242         frame->tf_ndirty = 0;
1243         frame->tf_cr_ifs = (1L<<63) | 3; /* sof=3, v=1 */
1244
1245         frame->tf_ar_rsc = 0xf; /* user mode rsc */
1246         frame->tf_ar_fpsr = IA64_FPSR_DEFAULT;
1247
1248         td->td_md.md_flags &= ~MDP_FPUSED;
1249         ia64_fpstate_drop(td);
1250 }
1251
1252 int
1253 ptrace_set_pc(struct thread *td, unsigned long addr)
1254 {
1255         /* TODO set pc in trapframe */
1256         return 0;
1257 }
1258
1259 int
1260 ptrace_single_step(struct thread *td)
1261 {
1262         /* TODO arrange for user process to single step */
1263         return 0;
1264 }
1265
1266 int
1267 ia64_pa_access(vm_offset_t pa)
1268 {
1269         return VM_PROT_READ|VM_PROT_WRITE;
1270 }
1271
1272 int
1273 fill_regs(td, regs)
1274         struct thread *td;
1275         struct reg *regs;
1276 {
1277         /* TODO copy trapframe to regs */
1278         return (0);
1279 }
1280
1281 int
1282 set_regs(td, regs)
1283         struct thread *td;
1284         struct reg *regs;
1285 {
1286         /* TODO copy regs to trapframe */
1287         return (0);
1288 }
1289
1290 int
1291 fill_dbregs(struct thread *td, struct dbreg *dbregs)
1292 {
1293
1294         return (ENOSYS);
1295 }
1296
1297 int
1298 set_dbregs(struct thread *td, struct dbreg *dbregs)
1299 {
1300
1301         return (ENOSYS);
1302 }
1303
1304 int
1305 fill_fpregs(td, fpregs)
1306         struct thread *td;
1307         struct fpreg *fpregs;
1308 {
1309         /* TODO copy fpu state to fpregs */
1310         ia64_fpstate_save(td, 0);
1311
1312 #if 0
1313         bcopy(&td->td_pcb->pcb_fp, fpregs, sizeof *fpregs);
1314 #endif
1315         return (0);
1316 }
1317
1318 int
1319 set_fpregs(td, fpregs)
1320         struct thread *td;
1321         struct fpreg *fpregs;
1322 {
1323         /* TODO copy fpregs fpu state */
1324         ia64_fpstate_drop(td);
1325
1326 #if 0
1327         bcopy(fpregs, &td->td_pcb->pcb_fp, sizeof *fpregs);
1328 #endif
1329         return (0);
1330 }
1331
1332 #ifndef DDB
1333 void
1334 Debugger(const char *msg)
1335 {
1336         printf("Debugger(\"%s\") called.\n", msg);
1337 }
1338 #endif /* no DDB */
1339
1340 #include <sys/disklabel.h>
1341
1342 /*
1343  * Determine the size of the transfer, and make sure it is
1344  * within the boundaries of the partition. Adjust transfer
1345  * if needed, and signal errors or early completion.
1346  */
1347 int
1348 bounds_check_with_label(struct bio *bp, struct disklabel *lp, int wlabel)
1349 {
1350 #if 0
1351         struct partition *p = lp->d_partitions + dkpart(bp->bio_dev);
1352         int labelsect = lp->d_partitions[0].p_offset;
1353         int maxsz = p->p_size,
1354                 sz = (bp->bio_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
1355
1356         /* overwriting disk label ? */
1357         /* XXX should also protect bootstrap in first 8K */
1358         if (bp->bio_blkno + p->p_offset <= LABELSECTOR + labelsect &&
1359 #if LABELSECTOR != 0
1360             bp->bio_blkno + p->p_offset + sz > LABELSECTOR + labelsect &&
1361 #endif
1362             (bp->bio_cmd == BIO_WRITE) && wlabel == 0) {
1363                 bp->bio_error = EROFS;
1364                 goto bad;
1365         }
1366
1367 #if     defined(DOSBBSECTOR) && defined(notyet)
1368         /* overwriting master boot record? */
1369         if (bp->bio_blkno + p->p_offset <= DOSBBSECTOR &&
1370             (bp->bio_cmd == BIO_WRITE) && wlabel == 0) {
1371                 bp->bio_error = EROFS;
1372                 goto bad;
1373         }
1374 #endif
1375
1376         /* beyond partition? */
1377         if (bp->bio_blkno < 0 || bp->bio_blkno + sz > maxsz) {
1378                 /* if exactly at end of disk, return an EOF */
1379                 if (bp->bio_blkno == maxsz) {
1380                         bp->bio_resid = bp->bio_bcount;
1381                         return(0);
1382                 }
1383                 /* or truncate if part of it fits */
1384                 sz = maxsz - bp->bio_blkno;
1385                 if (sz <= 0) {
1386                         bp->bio_error = EINVAL;
1387                         goto bad;
1388                 }
1389                 bp->bio_bcount = sz << DEV_BSHIFT;
1390         }
1391
1392         bp->bio_pblkno = bp->bio_blkno + p->p_offset;
1393         return(1);
1394
1395 bad:
1396 #endif
1397         bp->bio_flags |= BIO_ERROR;
1398         return(-1);
1399
1400 }
1401
1402 static int
1403 sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
1404 {
1405         int error;
1406         error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
1407                 req);
1408         if (!error && req->newptr)
1409                 resettodr();
1410         return (error);
1411 }
1412
1413 SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
1414         &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
1415
1416 SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set,
1417         CTLFLAG_RW, &disable_rtc_set, 0, "");
1418
1419 SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock,
1420         CTLFLAG_RW, &wall_cmos_clock, 0, "");
1421
1422 void
1423 ia64_fpstate_check(struct thread *td)
1424 {
1425         if ((td->td_frame->tf_cr_ipsr & IA64_PSR_DFH) == 0)
1426                 if (td != PCPU_GET(fpcurthread))
1427                         panic("ia64_check_fpcurthread: bogus");
1428 }
1429
1430 /*
1431  * Save the high floating point state in the pcb. Use this to get
1432  * read-only access to the floating point state. If write is true, the
1433  * current fp process is cleared so that fp state can safely be
1434  * modified. The process will automatically reload the changed state
1435  * by generating a disabled fp trap.
1436  */
1437 void
1438 ia64_fpstate_save(struct thread *td, int write)
1439 {
1440         if (td == PCPU_GET(fpcurthread)) {
1441                 /*
1442                  * Save the state in the pcb.
1443                  */
1444                 savehighfp(td->td_pcb->pcb_highfp);
1445
1446                 if (write) {
1447                         td->td_frame->tf_cr_ipsr |= IA64_PSR_DFH;
1448                         PCPU_SET(fpcurthread, NULL);
1449                 }
1450         }
1451 }
1452
1453 /*
1454  * Relinquish ownership of the FP state. This is called instead of
1455  * ia64_save_fpstate() if the entire FP state is being changed
1456  * (e.g. on sigreturn).
1457  */
1458 void
1459 ia64_fpstate_drop(struct thread *td)
1460 {
1461         if (td == PCPU_GET(fpcurthread)) {
1462                 td->td_frame->tf_cr_ipsr |= IA64_PSR_DFH;
1463                 PCPU_SET(fpcurthread, NULL);
1464         }
1465 }
1466
1467 /*
1468  * Switch the current owner of the fp state to p, reloading the state
1469  * from the pcb.
1470  */
1471 void
1472 ia64_fpstate_switch(struct thread *td)
1473 {
1474         if (PCPU_GET(fpcurthread)) {
1475                 /*
1476                  * Dump the old fp state if its valid.
1477                  */
1478                 savehighfp(PCPU_GET(fpcurthread)->td_pcb->pcb_highfp);
1479                 PCPU_GET(fpcurthread)->td_frame->tf_cr_ipsr |= IA64_PSR_DFH;
1480         }
1481
1482         /*
1483          * Remember the new FP owner and reload its state.
1484          */
1485         PCPU_SET(fpcurthread, td);
1486         restorehighfp(td->td_pcb->pcb_highfp);
1487         td->td_frame->tf_cr_ipsr &= ~IA64_PSR_DFH;
1488
1489         td->td_md.md_flags |= MDP_FPUSED;
1490 }
1491
1492 /*
1493  * Initialise a struct globaldata.
1494  */
1495 void
1496 globaldata_init(struct globaldata *globaldata, int cpuid, size_t sz)
1497 {
1498         bzero(globaldata, sz);
1499         globaldata->gd_cpuid = cpuid;
1500         globaldata_register(globaldata);
1501 }
1502
1503 /*
1504  * Utility functions for manipulating instruction bundles.
1505  */
1506 void
1507 ia64_unpack_bundle(u_int64_t low, u_int64_t high, struct ia64_bundle *bp)
1508 {
1509         bp->template = low & 0x1f;
1510         bp->slot[0] = (low >> 5) & ((1L<<41) - 1);
1511         bp->slot[1] = (low >> 46) | ((high & ((1L<<23) - 1)) << 18);
1512         bp->slot[2] = (high >> 23);
1513 }
1514
1515 void
1516 ia64_pack_bundle(u_int64_t *lowp, u_int64_t *highp,
1517                  const struct ia64_bundle *bp)
1518 {
1519         u_int64_t low, high;
1520
1521         low = bp->template | (bp->slot[0] << 5) | (bp->slot[1] << 46);
1522         high = (bp->slot[1] >> 18) | (bp->slot[2] << 23);
1523         *lowp = low;
1524         *highp = high;
1525 }
1526
1527 static int
1528 rse_slot(u_int64_t *bsp)
1529 {
1530         return ((u_int64_t) bsp >> 3) & 0x3f;
1531 }
1532
1533 /*
1534  * Return the address of register regno (regno >= 32) given that bsp
1535  * points at the base of the register stack frame.
1536  */
1537 u_int64_t *
1538 ia64_rse_register_address(u_int64_t *bsp, int regno)
1539 {
1540         int off = regno - 32;
1541         u_int64_t rnats = (rse_slot(bsp) + off) / 63;
1542         return bsp + off + rnats;
1543 }
1544
1545 /*
1546  * Calculate the base address of the previous frame given that the
1547  * current frame's locals area is 'size'.
1548  */
1549 u_int64_t *
1550 ia64_rse_previous_frame(u_int64_t *bsp, int size)
1551 {
1552         int slot = rse_slot(bsp);
1553         int rnats = 0;
1554         int count = size;
1555
1556         while (count > slot) {
1557                 count -= 63;
1558                 rnats++;
1559                 slot = 63;
1560         }
1561         return bsp - size - rnats;
1562 }
1563