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