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