2 * Copyright (c) 2000,2001 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include "opt_compat.h"
32 #include "opt_msgbuf.h"
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>
43 #include <sys/malloc.h>
44 #include <sys/reboot.h>
48 #include <sys/vmmeter.h>
49 #include <sys/msgbuf.h>
51 #include <sys/sysctl.h>
53 #include <sys/linker.h>
54 #include <sys/random.h>
56 #include <net/netisr.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>
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>
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>
86 extern void ia64_ski_init(void);
89 u_int64_t processor_frequency;
90 u_int64_t bus_frequency;
91 u_int64_t itc_frequency;
93 struct bootinfo bootinfo;
95 struct mtx sched_lock;
99 struct user *proc0uarea;
100 vm_offset_t proc0kstack;
102 extern u_int64_t kernel_text[], _end[];
103 extern u_int64_t _ia64_unwind_start[];
104 extern u_int64_t _ia64_unwind_end[];
106 FPSWA_INTERFACE *fpswa_interface;
108 u_int64_t ia64_pal_base;
109 u_int64_t ia64_port_base;
111 char machine[] = "ia64";
112 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
114 static char cpu_model[128];
115 SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD, cpu_model, 0, "");
118 /* start and end of kernel symbol table */
119 void *ksym_start, *ksym_end;
122 int ia64_unaligned_print = 1; /* warn about unaligned accesses */
123 int ia64_unaligned_fix = 1; /* fix up unaligned accesses */
124 int ia64_unaligned_sigbus = 0; /* don't SIGBUS on fixed-up accesses */
126 SYSCTL_INT(_machdep, CPU_UNALIGNED_PRINT, unaligned_print,
127 CTLFLAG_RW, &ia64_unaligned_print, 0, "");
129 SYSCTL_INT(_machdep, CPU_UNALIGNED_FIX, unaligned_fix,
130 CTLFLAG_RW, &ia64_unaligned_fix, 0, "");
132 SYSCTL_INT(_machdep, CPU_UNALIGNED_SIGBUS, unaligned_sigbus,
133 CTLFLAG_RW, &ia64_unaligned_sigbus, 0, "");
135 static void cpu_startup __P((void *));
136 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
138 struct msgbuf *msgbufp=0;
143 int totalphysmem; /* total amount of physical memory in system */
144 int physmem; /* physical memory used by NetBSD + some rsvd */
145 int resvmem; /* amount of memory reserved for PROM */
147 vm_offset_t phys_avail[100];
150 sysctl_hw_physmem(SYSCTL_HANDLER_ARGS)
152 int error = sysctl_handle_int(oidp, 0, ia64_ptob(physmem), req);
156 SYSCTL_PROC(_hw, HW_PHYSMEM, physmem, CTLTYPE_INT|CTLFLAG_RD,
157 0, 0, sysctl_hw_physmem, "I", "");
160 sysctl_hw_usermem(SYSCTL_HANDLER_ARGS)
162 int error = sysctl_handle_int(oidp, 0,
163 ia64_ptob(physmem - cnt.v_wire_count), req);
167 SYSCTL_PROC(_hw, HW_USERMEM, usermem, CTLTYPE_INT|CTLFLAG_RD,
168 0, 0, sysctl_hw_usermem, "I", "");
170 SYSCTL_INT(_hw, OID_AUTO, availpages, CTLFLAG_RD, &physmem, 0, "");
172 /* must be 2 less so 0 0 can signal end of chunks */
173 #define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2)
175 static void identifycpu __P((void));
177 struct kva_md_info kmi;
185 * Good {morning,afternoon,evening,night}.
189 /* startrtclock(); */
193 printf("real memory = %ld (%ldK bytes)\n", ia64_ptob(Maxmem), ia64_ptob(Maxmem) / 1024);
196 * Display any holes after the first chunk of extended memory.
201 printf("Physical memory chunk(s):\n");
202 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
203 int size1 = phys_avail[indx + 1] - phys_avail[indx];
205 printf("0x%08lx - 0x%08lx, %d bytes (%d pages)\n", phys_avail[indx],
206 phys_avail[indx + 1] - 1, size1, size1 / PAGE_SIZE);
210 vm_ksubmap_init(&kmi);
212 #if defined(USERCONFIG)
213 #if defined(USERCONFIG_BOOT)
216 if (boothowto & RB_CONFIG)
220 cninit(); /* the preferred console may have changed */
224 printf("avail memory = %ld (%ldK bytes)\n", ptoa(cnt.v_free_count),
225 ptoa(cnt.v_free_count) / 1024);
227 if (fpswa_interface == NULL)
228 printf("Warning: no FPSWA package supplied\n");
230 printf("FPSWA Revision = 0x%lx, Entry = %p\n",
231 (long)fpswa_interface->Revision,
232 (void *)fpswa_interface->Fpswa);
235 * Set up buffers, so they can be used to read disk labels.
238 vm_pager_bufferinit();
242 * Traverse the MADT to discover IOSAPIC and Local SAPIC
250 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
252 KASSERT(size >= sizeof(struct pcpu) + sizeof(struct pcb),
253 ("%s: too small an allocation for pcpu", __func__));
254 pcpu->pc_pcb = (void*)(pcpu+1);
262 int number, revision, model, family, archrev;
266 * Assumes little-endian.
268 *(u_int64_t *) &vendor[0] = ia64_get_cpuid(0);
269 *(u_int64_t *) &vendor[8] = ia64_get_cpuid(1);
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;
280 strcpy(cpu_model, "Itanium");
281 else if (family == 0x1f)
282 strcpy(cpu_model, "McKinley");
284 snprintf(cpu_model, sizeof(cpu_model), "Family=%d", family);
286 features = ia64_get_cpuid(4);
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);
295 printf(" Origin = \"%s\" Model = %d Revision = %d\n",
296 vendor, model, revision);
297 printf(" Features = 0x%b\n", (u_int32_t) features,
303 add_kernel_unwind_tables(void *arg)
306 * Register the kernel's unwind table.
308 ia64_add_unwind_table(kernel_text,
312 SYSINIT(unwind, SI_SUB_KMEM, SI_ORDER_ANY, add_kernel_unwind_tables, 0);
320 if (ia64_pal_base == 0)
323 bzero(&pte, sizeof(pte));
325 pte.pte_ma = PTE_MA_WB;
328 pte.pte_pl = PTE_PL_KERN;
329 pte.pte_ar = PTE_AR_RWX;
330 pte.pte_ppn = ia64_pal_base >> 12;
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));
345 calculate_frequencies(void)
347 struct ia64_sal_result sal;
348 struct ia64_pal_result pal;
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);
353 if (sal.sal_status == 0 && pal.pal_status == 0) {
355 printf("Platform clock frequency %ld Hz\n",
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));
366 processor_frequency =
367 sal.sal_result[0] * (pal.pal_result[0] >> 32)
368 / (pal.pal_result[0] & ((1L << 32) - 1));
370 sal.sal_result[0] * (pal.pal_result[1] >> 32)
371 / (pal.pal_result[1] & ((1L << 32) - 1));
373 sal.sal_result[0] * (pal.pal_result[2] >> 32)
374 / (pal.pal_result[2] & ((1L << 32) - 1));
379 ia64_init(u_int64_t arg1, u_int64_t arg2)
382 vm_offset_t kernstart, kernend;
383 vm_offset_t kernstartpfn, kernendpfn, pfn0, pfn1;
385 EFI_MEMORY_DESCRIPTOR *md, *mdp;
388 /* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
391 * TODO: Disable interrupts, floating point etc.
392 * Maybe flush cache and tlb
394 ia64_set_fpsr(IA64_FPSR_DEFAULT);
397 * TODO: Get critical system information (if possible, from the
398 * information provided by the boot program).
402 * Gross and disgusting hack. The bootinfo is written into
403 * memory at a fixed address.
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);
413 * Look for the I/O ports first - we need them for console
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) {
420 static EFI_MEMORY_DESCRIPTOR ski_md[2];
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
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;
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;
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;
451 KASSERT(ia64_port_base != 0,
452 ("%s: no I/O memory region", __func__));
455 * Look at arguments passed to us and compute boothowto.
457 boothowto = bootinfo.bi_boothowto;
463 * Catch case of boot_verbose set in environment.
465 if ((p = getenv("boot_verbose")) != NULL) {
466 if (strcmp(p, "yes") == 0 || strcmp(p, "YES") == 0) {
467 boothowto |= RB_VERBOSE;
471 if (boothowto & RB_VERBOSE)
475 * Initialize the console before we print anything out.
479 /* OUTPUT NOW ALLOWED */
481 if (ia64_pal_base != 0) {
482 ia64_pal_base &= ~((1 << 28) - 1);
484 * We use a TR to map the first 256M of memory - this might
485 * cover the palcode too.
487 if (ia64_pal_base == 0)
488 printf("PAL code mapped by the kernel's TR\n");
490 printf("PAL code not found\n");
493 * Wire things up so we can call the firmware.
500 calculate_frequencies();
503 * Find the beginning and end of the kernel.
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;
514 kern_envp = static_env;
516 kern_envp = (caddr_t)bootinfo.bi_envp;
518 /* get fpswa interface */
519 fpswa_interface = (FPSWA_INTERFACE*)IA64_PHYS_TO_RR7(bootinfo.bi_fpswa);
521 /* Init basic tunables, including hz */
524 p = getenv("kernelname");
526 strncpy(kernelname, p, sizeof(kernelname) - 1);
528 kernstartpfn = atop(IA64_RR_MASK(kernstart));
529 kernendpfn = atop(IA64_RR_MASK(kernend));
532 * Size the memory regions and load phys_avail[] with the results.
536 * Find out how much memory is available, by looking at
537 * the memory descriptors.
541 printf("Memory descriptor count: %d\n", mdcount);
545 for (i = 0, mdp = md; i < mdcount; i++,
546 mdp = NextMemoryDescriptor(mdp, bootinfo.bi_memdesc_size)) {
548 printf("MD %d: type %d pa 0x%lx cnt 0x%lx\n", i,
554 pfn0 = ia64_btop(round_page(mdp->PhysicalStart));
555 pfn1 = ia64_btop(trunc_page(mdp->PhysicalStart
556 + mdp->NumberOfPages * 4096));
560 if (mdp->Type != EfiConventionalMemory) {
561 resvmem += (pfn1 - pfn0);
565 totalphysmem += (pfn1 - pfn0);
568 * We have a memory descriptors available for system
569 * software use. We must determine if this cluster
572 physmem += (pfn1 - pfn0);
573 if (pfn0 <= kernendpfn && kernstartpfn <= pfn1) {
575 * Must compute the location of the kernel
576 * within the segment.
579 printf("Descriptor %d contains kernel\n", i);
581 if (pfn0 < kernstartpfn) {
583 * There is a chunk before the kernel.
586 printf("Loading chunk before kernel: "
587 "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
589 phys_avail[phys_avail_cnt] = ia64_ptob(pfn0);
590 phys_avail[phys_avail_cnt+1] = ia64_ptob(kernstartpfn);
593 if (kernendpfn < pfn1) {
595 * There is a chunk after the kernel.
598 printf("Loading chunk after kernel: "
599 "0x%lx / 0x%lx\n", kernendpfn, pfn1);
601 phys_avail[phys_avail_cnt] = ia64_ptob(kernendpfn);
602 phys_avail[phys_avail_cnt+1] = ia64_ptob(pfn1);
607 * Just load this cluster as one chunk.
610 printf("Loading descriptor %d: 0x%lx / 0x%lx\n", i,
613 phys_avail[phys_avail_cnt] = ia64_ptob(pfn0);
614 phys_avail[phys_avail_cnt+1] = ia64_ptob(pfn1);
619 phys_avail[phys_avail_cnt] = 0;
622 init_param2(physmem);
625 * Initialize error message buffer (at end of core).
628 size_t sz = round_page(MSGBUF_SIZE);
629 int i = phys_avail_cnt - 2;
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];
635 phys_avail[i+1] -= sz;
636 msgbufp = (struct msgbuf*) IA64_PHYS_TO_RR7(phys_avail[i+1]);
638 msgbufinit(msgbufp, sz);
640 /* Remove the last segment if it now has no pages. */
641 if (phys_avail[i] == phys_avail[i+1]) {
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);
653 proc_linkup(&proc0, &proc0.p_ksegrp, &proc0.p_kse, &thread0);
655 * Init mapping for u page(s) for proc 0
657 proc0uarea = (struct user *)pmap_steal_memory(UAREA_PAGES * PAGE_SIZE);
658 proc0kstack = (vm_offset_t)kstack;
659 proc0.p_uarea = proc0uarea;
660 thread0.td_kstack = proc0kstack;
661 thread0.td_pcb = (struct pcb *)
662 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
664 * Setup the global data for the bootstrap cpu.
666 pcpup = (struct pcpu *) pmap_steal_memory(PAGE_SIZE);
667 pcpu_init(pcpup, 0, PAGE_SIZE);
668 ia64_set_k4((u_int64_t) pcpup);
671 * Initialize the rest of proc 0's PCB.
673 * Set the kernel sp, reserving space for an (empty) trapframe,
674 * and make proc0's trapframe pointer point to it for sanity.
675 * Initialise proc0's backing store to start after u area.
677 * XXX what is all this +/- 16 stuff?
679 thread0.td_frame = (struct trapframe *)thread0.td_pcb - 1;
680 thread0.td_pcb->pcb_sp = (u_int64_t)thread0.td_frame - 16;
681 thread0.td_pcb->pcb_bspstore = (u_int64_t)proc0kstack;
683 /* Setup curproc so that mutexes work */
684 PCPU_SET(curthread, &thread0);
686 /* We pretend to own FP state so that ia64_fpstate_check() works */
687 PCPU_SET(fpcurthread, &thread0);
689 LIST_INIT(&thread0.td_contested);
692 * Initialise mutexes.
694 mtx_init(&Giant, "Giant", MTX_DEF | MTX_RECURSE);
695 mtx_init(&sched_lock, "sched lock", MTX_SPIN | MTX_RECURSE);
696 mtx_init(&proc0.p_mtx, "process lock", MTX_DEF);
700 * Initialize the virtual memory system.
705 * Initialize debuggers, and break into them if appropriate.
709 if (boothowto & RB_KDB) {
710 printf("Boot flags requested debugger\n");
717 ia64_running_in_simulator()
719 return bootinfo.bi_systab == 0;
723 bzero(void *buf, size_t len)
727 while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
731 while (len >= sizeof(u_long) * 8) {
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;
743 while (len >= sizeof(u_long)) {
745 len -= sizeof(u_long);
757 u_int64_t start, end, now;
759 start = ia64_get_itc();
760 end = start + (itc_frequency * n) / 1000000;
761 /* printf("DELAY from 0x%lx to 0x%lx\n", start, end); */
763 now = ia64_get_itc();
764 } while (now < end || (now > start && end < start));
768 * Send an interrupt to process.
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
778 sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
782 struct trapframe *frame;
784 struct sigframe sf, *sfp;
786 int oonstack, rndfsize;
790 PROC_LOCK_ASSERT(p, MA_OWNED);
792 frame = td->td_frame;
793 oonstack = sigonstack(frame->tf_r[FRAME_SP]);
794 rndfsize = ((sizeof(sf) + 15) / 16) * 16;
797 * Make sure that we restore the entire trapframe after a
800 frame->tf_flags &= ~FRAME_SYSCALL;
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;
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;
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));
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'.
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);
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;
857 sfp = (struct sigframe *)(frame->tf_r[FRAME_SP] - rndfsize);
860 (void)grow_stack(p, (u_long)sfp);
862 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
863 printf("sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
866 if (!useracc((caddr_t)sfp, sizeof(sf), VM_PROT_WRITE)) {
868 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
869 printf("sendsig(%d): useracc failed on sig %d\n",
873 * Process has trashed its stack; give it an illegal
874 * instruction to halt it in its tracks.
877 SIGACTION(p, SIGILL) = SIG_DFL;
878 SIGDELSET(p->p_sigignore, SIGILL);
879 SIGDELSET(p->p_sigcatch, SIGILL);
880 SIGDELSET(p->p_sigmask, SIGILL);
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;
896 * copy the frame out to userland.
898 (void) copyout((caddr_t)&sf, (caddr_t)sfp, sizeof(sf));
900 if (sigdebug & SDB_FOLLOW)
901 printf("sendsig(%d): sig %d sfp %p code %lx\n", p->p_pid, sig,
906 * Set up the registers to return to sigcode.
908 frame->tf_cr_ipsr &= ~IA64_PSR_RI;
909 frame->tf_cr_iip = PS_STRINGS - (esigcode - sigcode);
910 frame->tf_r[FRAME_R1] = sig;
912 if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) {
913 frame->tf_r[FRAME_R15] = (u_int64_t)&(sfp->sf_si);
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;
921 frame->tf_r[FRAME_R15] = code;
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;
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",
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.
946 osigreturn(struct thread *td, struct osigreturn_args *uap)
949 return (nosys(td, (struct nosys_args *)uap));
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.
962 sigreturn(struct thread *td,
963 struct sigreturn_args /* {
964 ucontext_t *sigcntxp;
968 const ucontext_t *ucp;
970 struct trapframe *frame = td->td_frame;
971 struct __mcontext *mcp;
979 if (sigdebug & SDB_FOLLOW)
980 printf("sigreturn: pid %d, scp %p\n", p->p_pid, ucp);
984 * Fetch the entire context structure at once for speed.
985 * We don't use a normal argument to simplify RSE handling.
987 if (copyin((caddr_t)frame->tf_r[FRAME_R4],
988 (caddr_t)&uc, sizeof(ucontext_t)))
991 if (frame->tf_ndirty != 0) {
992 printf("sigreturn: dirty user stacked registers\n");
996 * Restore the user-supplied information
998 mcp = &uc.uc_mcontext;
999 bcopy(&mcp->mc_br[0], &frame->tf_b[0], 8*sizeof(u_int64_t));
1000 bcopy(&mcp->mc_gr[1], &frame->tf_r[0], 31*sizeof(u_int64_t));
1003 frame->tf_flags &= ~FRAME_SYSCALL;
1004 frame->tf_cr_iip = mcp->mc_ip & ~15;
1005 frame->tf_cr_ipsr &= ~IA64_PSR_RI;
1006 switch (mcp->mc_ip & 15) {
1008 frame->tf_cr_ipsr |= IA64_PSR_RI_1;
1011 frame->tf_cr_ipsr |= IA64_PSR_RI_2;
1014 frame->tf_cr_ipsr = ((frame->tf_cr_ipsr & ~0x1fff)
1015 | (mcp->mc_um & 0x1fff));
1016 frame->tf_pr = mcp->mc_pr;
1017 frame->tf_ar_rsc = (mcp->mc_ar_rsc & 3) | 12; /* user, loadrs=0 */
1018 frame->tf_ar_pfs = mcp->mc_ar_pfs;
1019 frame->tf_cr_ifs = mcp->mc_cfm | (1UL<<63);
1020 frame->tf_ar_bspstore = mcp->mc_ar_bsp;
1021 frame->tf_ar_rnat = mcp->mc_ar_rnat;
1022 frame->tf_ndirty = 0; /* assumes flushrs in sigcode */
1023 frame->tf_ar_unat = mcp->mc_ar_unat;
1024 frame->tf_ar_ccv = mcp->mc_ar_ccv;
1025 frame->tf_ar_fpsr = mcp->mc_ar_fpsr;
1027 frame->tf_r[FRAME_SP] = mcp->mc_sp;
1030 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1031 if (uc.uc_mcontext.mc_onstack & 1)
1032 p->p_sigstk.ss_flags |= SS_ONSTACK;
1034 p->p_sigstk.ss_flags &= ~SS_ONSTACK;
1037 p->p_sigmask = uc.uc_sigmask;
1038 SIG_CANTMASK(p->p_sigmask);
1041 /* XXX ksc.sc_ownedfp ? */
1042 ia64_fpstate_drop(td);
1044 bcopy((struct fpreg *)uc.uc_mcontext.mc_fpregs,
1045 &td->td_pcb->pcb_fp, sizeof(struct fpreg));
1046 td->td_pcb->pcb_fp_control = uc.uc_mcontext.mc_fp_control;
1050 if (sigdebug & SDB_FOLLOW)
1051 printf("sigreturn(%d): returns\n", p->p_pid);
1053 return (EJUSTRETURN);
1057 * Machine dependent boot() routine
1063 ia64_efi_runtime->ResetSystem(EfiResetWarm, EFI_SUCCESS, 0, 0);
1067 * Shutdown the CPU as much as possible
1073 ia64_efi_runtime->ResetSystem(EfiResetWarm, EFI_SUCCESS, 0, 0);
1077 * Clear registers on exec
1080 setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
1082 struct trapframe *frame;
1084 frame = td->td_frame;
1087 * Make sure that we restore the entire trapframe after an
1090 frame->tf_flags &= ~FRAME_SYSCALL;
1092 bzero(frame->tf_r, sizeof(frame->tf_r));
1093 bzero(frame->tf_f, sizeof(frame->tf_f));
1094 frame->tf_cr_iip = entry;
1095 frame->tf_cr_ipsr = (IA64_PSR_IC
1102 | IA64_PSR_CPL_USER);
1104 * Make sure that sp is aligned to a 16 byte boundary and
1105 * reserve 16 bytes of scratch space for _start.
1107 frame->tf_r[FRAME_SP] = (stack & ~15) - 16;
1110 * Write values for out0, out1 and out2 to the user's backing
1111 * store and arrange for them to be restored into the user's
1112 * initial register frame. Assumes that (bspstore & 0x1f8) <
1115 frame->tf_ar_bspstore = td->td_md.md_bspstore + 24;
1116 suword((caddr_t) frame->tf_ar_bspstore - 24, stack);
1117 suword((caddr_t) frame->tf_ar_bspstore - 16, ps_strings);
1118 suword((caddr_t) frame->tf_ar_bspstore - 8, 0);
1119 frame->tf_ndirty = 0;
1120 frame->tf_cr_ifs = (1L<<63) | 3; /* sof=3, v=1 */
1122 frame->tf_ar_rsc = 0xf; /* user mode rsc */
1123 frame->tf_ar_fpsr = IA64_FPSR_DEFAULT;
1125 td->td_md.md_flags &= ~MDP_FPUSED;
1126 ia64_fpstate_drop(td);
1130 ptrace_set_pc(struct thread *td, unsigned long addr)
1132 /* TODO set pc in trapframe */
1137 ptrace_single_step(struct thread *td)
1139 /* TODO arrange for user process to single step */
1144 ia64_pa_access(vm_offset_t pa)
1146 return VM_PROT_READ|VM_PROT_WRITE;
1154 /* TODO copy trapframe to regs */
1163 /* TODO copy regs to trapframe */
1168 fill_dbregs(struct thread *td, struct dbreg *dbregs)
1175 set_dbregs(struct thread *td, struct dbreg *dbregs)
1182 fill_fpregs(td, fpregs)
1184 struct fpreg *fpregs;
1186 /* TODO copy fpu state to fpregs */
1187 ia64_fpstate_save(td, 0);
1190 bcopy(&td->td_pcb->pcb_fp, fpregs, sizeof *fpregs);
1196 set_fpregs(td, fpregs)
1198 struct fpreg *fpregs;
1200 /* TODO copy fpregs fpu state */
1201 ia64_fpstate_drop(td);
1204 bcopy(fpregs, &td->td_pcb->pcb_fp, sizeof *fpregs);
1211 Debugger(const char *msg)
1213 printf("Debugger(\"%s\") called.\n", msg);
1217 #include <sys/disklabel.h>
1220 * Determine the size of the transfer, and make sure it is
1221 * within the boundaries of the partition. Adjust transfer
1222 * if needed, and signal errors or early completion.
1225 bounds_check_with_label(struct bio *bp, struct disklabel *lp, int wlabel)
1228 struct partition *p = lp->d_partitions + dkpart(bp->bio_dev);
1229 int labelsect = lp->d_partitions[0].p_offset;
1230 int maxsz = p->p_size,
1231 sz = (bp->bio_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
1233 /* overwriting disk label ? */
1234 /* XXX should also protect bootstrap in first 8K */
1235 if (bp->bio_blkno + p->p_offset <= LABELSECTOR + labelsect &&
1236 #if LABELSECTOR != 0
1237 bp->bio_blkno + p->p_offset + sz > LABELSECTOR + labelsect &&
1239 (bp->bio_cmd == BIO_WRITE) && wlabel == 0) {
1240 bp->bio_error = EROFS;
1244 #if defined(DOSBBSECTOR) && defined(notyet)
1245 /* overwriting master boot record? */
1246 if (bp->bio_blkno + p->p_offset <= DOSBBSECTOR &&
1247 (bp->bio_cmd == BIO_WRITE) && wlabel == 0) {
1248 bp->bio_error = EROFS;
1253 /* beyond partition? */
1254 if (bp->bio_blkno < 0 || bp->bio_blkno + sz > maxsz) {
1255 /* if exactly at end of disk, return an EOF */
1256 if (bp->bio_blkno == maxsz) {
1257 bp->bio_resid = bp->bio_bcount;
1260 /* or truncate if part of it fits */
1261 sz = maxsz - bp->bio_blkno;
1263 bp->bio_error = EINVAL;
1266 bp->bio_bcount = sz << DEV_BSHIFT;
1269 bp->bio_pblkno = bp->bio_blkno + p->p_offset;
1274 bp->bio_flags |= BIO_ERROR;
1280 sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
1283 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
1285 if (!error && req->newptr)
1290 SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
1291 &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
1293 SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set,
1294 CTLFLAG_RW, &disable_rtc_set, 0, "");
1296 SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock,
1297 CTLFLAG_RW, &wall_cmos_clock, 0, "");
1300 ia64_fpstate_check(struct thread *td)
1302 if ((td->td_frame->tf_cr_ipsr & IA64_PSR_DFH) == 0)
1303 if (td != PCPU_GET(fpcurthread))
1304 panic("ia64_fpstate_check: bogus");
1308 * Save the high floating point state in the pcb. Use this to get
1309 * read-only access to the floating point state. If write is true, the
1310 * current fp process is cleared so that fp state can safely be
1311 * modified. The process will automatically reload the changed state
1312 * by generating a disabled fp trap.
1315 ia64_fpstate_save(struct thread *td, int write)
1317 if (td == PCPU_GET(fpcurthread)) {
1319 * Save the state in the pcb.
1321 savehighfp(td->td_pcb->pcb_highfp);
1324 td->td_frame->tf_cr_ipsr |= IA64_PSR_DFH;
1325 PCPU_SET(fpcurthread, NULL);
1331 * Relinquish ownership of the FP state. This is called instead of
1332 * ia64_save_fpstate() if the entire FP state is being changed
1333 * (e.g. on sigreturn).
1336 ia64_fpstate_drop(struct thread *td)
1338 if (td == PCPU_GET(fpcurthread)) {
1339 td->td_frame->tf_cr_ipsr |= IA64_PSR_DFH;
1340 PCPU_SET(fpcurthread, NULL);
1345 * Switch the current owner of the fp state to p, reloading the state
1349 ia64_fpstate_switch(struct thread *td)
1351 if (PCPU_GET(fpcurthread)) {
1353 * Dump the old fp state if its valid.
1355 savehighfp(PCPU_GET(fpcurthread)->td_pcb->pcb_highfp);
1356 PCPU_GET(fpcurthread)->td_frame->tf_cr_ipsr |= IA64_PSR_DFH;
1360 * Remember the new FP owner and reload its state.
1362 PCPU_SET(fpcurthread, td);
1363 restorehighfp(td->td_pcb->pcb_highfp);
1364 td->td_frame->tf_cr_ipsr &= ~IA64_PSR_DFH;
1366 td->td_md.md_flags |= MDP_FPUSED;
1370 * Utility functions for manipulating instruction bundles.
1373 ia64_unpack_bundle(u_int64_t low, u_int64_t high, struct ia64_bundle *bp)
1375 bp->template = low & 0x1f;
1376 bp->slot[0] = (low >> 5) & ((1L<<41) - 1);
1377 bp->slot[1] = (low >> 46) | ((high & ((1L<<23) - 1)) << 18);
1378 bp->slot[2] = (high >> 23);
1382 ia64_pack_bundle(u_int64_t *lowp, u_int64_t *highp,
1383 const struct ia64_bundle *bp)
1385 u_int64_t low, high;
1387 low = bp->template | (bp->slot[0] << 5) | (bp->slot[1] << 46);
1388 high = (bp->slot[1] >> 18) | (bp->slot[2] << 23);
1394 rse_slot(u_int64_t *bsp)
1396 return ((u_int64_t) bsp >> 3) & 0x3f;
1400 * Return the address of register regno (regno >= 32) given that bsp
1401 * points at the base of the register stack frame.
1404 ia64_rse_register_address(u_int64_t *bsp, int regno)
1406 int off = regno - 32;
1407 u_int64_t rnats = (rse_slot(bsp) + off) / 63;
1408 return bsp + off + rnats;
1412 * Calculate the base address of the previous frame given that the
1413 * current frame's locals area is 'size'.
1416 ia64_rse_previous_frame(u_int64_t *bsp, int size)
1418 int slot = rse_slot(bsp);
1422 while (count > slot) {
1427 return bsp - size - rnats;