2 * Copyright (c) 2003,2004 Marcel Moolenaar
3 * Copyright (c) 2000,2001 Doug Rabson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_compat.h"
33 #include "opt_kstack_pages.h"
34 #include "opt_msgbuf.h"
36 #include <sys/param.h>
38 #include <sys/systm.h>
44 #include <sys/eventhandler.h>
46 #include <sys/imgact.h>
48 #include <sys/kernel.h>
49 #include <sys/linker.h>
51 #include <sys/malloc.h>
53 #include <sys/msgbuf.h>
55 #include <sys/ptrace.h>
56 #include <sys/random.h>
57 #include <sys/reboot.h>
58 #include <sys/sched.h>
59 #include <sys/signalvar.h>
60 #include <sys/syscall.h>
61 #include <sys/sysctl.h>
62 #include <sys/sysproto.h>
63 #include <sys/ucontext.h>
66 #include <sys/vmmeter.h>
67 #include <sys/vnode.h>
71 #include <net/netisr.h>
74 #include <vm/vm_extern.h>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_map.h>
78 #include <vm/vm_object.h>
79 #include <vm/vm_pager.h>
81 #include <machine/bootinfo.h>
82 #include <machine/clock.h>
83 #include <machine/cpu.h>
84 #include <machine/efi.h>
85 #include <machine/elf.h>
86 #include <machine/fpu.h>
87 #include <machine/mca.h>
88 #include <machine/md_var.h>
89 #include <machine/mutex.h>
90 #include <machine/pal.h>
91 #include <machine/pcb.h>
92 #include <machine/reg.h>
93 #include <machine/sal.h>
94 #include <machine/sigframe.h>
96 #include <machine/smp.h>
98 #include <machine/unwind.h>
99 #include <machine/vmparam.h>
101 #include <i386/include/specialreg.h>
103 u_int64_t processor_frequency;
104 u_int64_t bus_frequency;
105 u_int64_t itc_frequency;
108 u_int64_t pa_bootinfo;
109 struct bootinfo bootinfo;
112 extern char kstack[];
113 vm_offset_t proc0kstack;
115 extern u_int64_t kernel_text[], _end[];
117 extern u_int64_t ia64_gateway_page[];
118 extern u_int64_t break_sigtramp[];
119 extern u_int64_t epc_sigtramp[];
121 struct fpswa_iface *fpswa_iface;
123 u_int64_t ia64_pal_base;
124 u_int64_t ia64_port_base;
126 static int ia64_inval_icache_needed;
128 char machine[] = MACHINE;
129 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
131 static char cpu_model[64];
132 SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD, cpu_model, 0,
133 "The CPU model name");
135 static char cpu_family[64];
136 SYSCTL_STRING(_hw, OID_AUTO, family, CTLFLAG_RD, cpu_family, 0,
137 "The CPU family name");
140 extern vm_offset_t ksym_start, ksym_end;
143 static void cpu_startup(void *);
144 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
146 struct msgbuf *msgbufp=0;
151 #define PHYSMAP_SIZE (2 * VM_PHYSSEG_MAX)
153 vm_paddr_t phys_avail[PHYSMAP_SIZE + 2];
155 /* must be 2 less so 0 0 can signal end of chunks */
156 #define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2)
158 void mi_startup(void); /* XXX should be in a MI header */
160 struct kva_md_info kmi;
163 #define Ghz (1000L*Mhz)
165 void setPQL2(int *const size, int *const ways);
168 setPQL2(int *const size, int *const ways)
177 char *family_name, *model_name;
178 u_int64_t features, tmp;
179 int number, revision, model, family, archrev;
182 * Assumes little-endian.
184 *(u_int64_t *) &vendor[0] = ia64_get_cpuid(0);
185 *(u_int64_t *) &vendor[8] = ia64_get_cpuid(1);
188 tmp = ia64_get_cpuid(3);
189 number = (tmp >> 0) & 0xff;
190 revision = (tmp >> 8) & 0xff;
191 model = (tmp >> 16) & 0xff;
192 family = (tmp >> 24) & 0xff;
193 archrev = (tmp >> 32) & 0xff;
195 family_name = model_name = "unknown";
198 family_name = "Itanium";
199 model_name = "Merced";
202 family_name = "Itanium 2";
205 model_name = "McKinley";
209 * Deerfield is a low-voltage variant based on the
210 * Madison core. We need circumstantial evidence
211 * (i.e. the clock frequency) to identify those.
212 * Allow for roughly 1% error margin.
214 tmp = processor_frequency >> 7;
215 if ((processor_frequency - tmp) < 1*Ghz &&
216 (processor_frequency + tmp) >= 1*Ghz)
217 model_name = "Deerfield";
219 model_name = "Madison";
222 model_name = "Madison II";
227 ia64_inval_icache_needed = 1;
229 family_name = "Itanium 2";
232 model_name = "Montecito";
237 snprintf(cpu_family, sizeof(cpu_family), "%s", family_name);
238 snprintf(cpu_model, sizeof(cpu_model), "%s", model_name);
240 features = ia64_get_cpuid(4);
242 printf("CPU: %s (", model_name);
243 if (processor_frequency) {
244 printf("%ld.%02ld-Mhz ",
245 (processor_frequency + 4999) / Mhz,
246 ((processor_frequency + 4999) / (Mhz/100)) % 100);
248 printf("%s)\n", family_name);
249 printf(" Origin = \"%s\" Revision = %d\n", vendor, revision);
250 printf(" Features = 0x%b\n", (u_int32_t) features,
252 "\001LB" /* long branch (brl) instruction. */
253 "\002SD" /* Spontaneous deferral. */
254 "\003AO" /* 16-byte atomic operations (ld, st, cmpxchg). */ );
263 * Good {morning,afternoon,evening,night}.
267 /* startrtclock(); */
271 printf("real memory = %ld (%ld MB)\n", ia64_ptob(Maxmem),
272 ia64_ptob(Maxmem) / 1048576);
276 * Display any holes after the first chunk of extended memory.
281 printf("Physical memory chunk(s):\n");
282 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
283 long size1 = phys_avail[indx + 1] - phys_avail[indx];
285 printf("0x%08lx - 0x%08lx, %ld bytes (%ld pages)\n",
286 phys_avail[indx], phys_avail[indx + 1] - 1, size1,
287 size1 >> PAGE_SHIFT);
291 vm_ksubmap_init(&kmi);
293 printf("avail memory = %ld (%ld MB)\n", ptoa(cnt.v_free_count),
294 ptoa(cnt.v_free_count) / 1048576);
296 if (fpswa_iface == NULL)
297 printf("Warning: no FPSWA package supplied\n");
299 printf("FPSWA Revision = 0x%lx, Entry = %p\n",
300 (long)fpswa_iface->if_rev, (void *)fpswa_iface->if_fpswa);
303 * Set up buffers, so they can be used to read disk labels.
306 vm_pager_bufferinit();
309 * Traverse the MADT to discover IOSAPIC and Local SAPIC
323 /* Get current clock frequency for the given cpu id. */
325 cpu_est_clockrate(int cpu_id, uint64_t *rate)
328 if (pcpu_find(cpu_id) == NULL || rate == NULL)
330 *rate = processor_frequency;
342 cpu_idle_default(void)
344 struct ia64_pal_result res;
346 res = ia64_call_pal_static(PAL_HALT_LIGHT, 0, 0, 0);
355 /* Other subsystems (e.g., ACPI) can hook this later. */
356 void (*cpu_idle_hook)(void) = cpu_idle_default;
366 cpu_switch(struct thread *old, struct thread *new, struct mtx *mtx)
368 struct pcb *oldpcb, *newpcb;
370 oldpcb = old->td_pcb;
372 ia32_savectx(oldpcb);
374 if (PCPU_GET(fpcurthread) == old)
375 old->td_frame->tf_special.psr |= IA64_PSR_DFH;
376 if (!savectx(oldpcb)) {
377 newpcb = new->td_pcb;
378 oldpcb->pcb_current_pmap =
379 pmap_switch(newpcb->pcb_current_pmap);
380 PCPU_SET(curthread, new);
382 ia32_restorectx(newpcb);
384 if (PCPU_GET(fpcurthread) == new)
385 new->td_frame->tf_special.psr &= ~IA64_PSR_DFH;
387 /* We should not get here. */
388 panic("cpu_switch: restorectx() returned");
394 cpu_throw(struct thread *old __unused, struct thread *new)
398 newpcb = new->td_pcb;
399 (void)pmap_switch(newpcb->pcb_current_pmap);
400 PCPU_SET(curthread, new);
402 ia32_restorectx(newpcb);
405 /* We should not get here. */
406 panic("cpu_throw: restorectx() returned");
411 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
414 pcpu->pc_acpi_id = cpuid;
423 if (td->td_md.md_spinlock_count == 0)
424 td->td_md.md_saved_intr = intr_disable();
425 td->td_md.md_spinlock_count++;
436 td->td_md.md_spinlock_count--;
437 if (td->td_md.md_spinlock_count == 0)
438 intr_restore(td->td_md.md_saved_intr);
442 map_vhpt(uintptr_t vhpt)
447 pte = PTE_PRESENT | PTE_MA_WB | PTE_ACCESSED | PTE_DIRTY |
448 PTE_PL_KERN | PTE_AR_RW;
449 pte |= vhpt & PTE_PPN_MASK;
451 __asm __volatile("ptr.d %0,%1" :: "r"(vhpt),
452 "r"(IA64_ID_PAGE_SHIFT<<2));
454 __asm __volatile("mov %0=psr" : "=r"(psr));
455 __asm __volatile("rsm psr.ic|psr.i");
458 ia64_set_itir(IA64_ID_PAGE_SHIFT << 2);
460 __asm __volatile("itr.d dtr[%0]=%1" :: "r"(2), "r"(pte));
461 __asm __volatile("mov psr.l=%0" :: "r" (psr));
471 if (ia64_pal_base == 0)
474 pte = PTE_PRESENT | PTE_MA_WB | PTE_ACCESSED | PTE_DIRTY |
475 PTE_PL_KERN | PTE_AR_RWX;
476 pte |= ia64_pal_base & PTE_PPN_MASK;
478 __asm __volatile("ptr.d %0,%1; ptr.i %0,%1" ::
479 "r"(IA64_PHYS_TO_RR7(ia64_pal_base)), "r"(IA64_ID_PAGE_SHIFT<<2));
481 __asm __volatile("mov %0=psr" : "=r"(psr));
482 __asm __volatile("rsm psr.ic|psr.i");
484 ia64_set_ifa(IA64_PHYS_TO_RR7(ia64_pal_base));
485 ia64_set_itir(IA64_ID_PAGE_SHIFT << 2);
487 __asm __volatile("itr.d dtr[%0]=%1" :: "r"(1), "r"(pte));
489 __asm __volatile("itr.i itr[%0]=%1" :: "r"(1), "r"(pte));
490 __asm __volatile("mov psr.l=%0" :: "r" (psr));
495 map_gateway_page(void)
500 pte = PTE_PRESENT | PTE_MA_WB | PTE_ACCESSED | PTE_DIRTY |
501 PTE_PL_KERN | PTE_AR_X_RX;
502 pte |= (uint64_t)ia64_gateway_page & PTE_PPN_MASK;
504 __asm __volatile("ptr.d %0,%1; ptr.i %0,%1" ::
505 "r"(VM_MAX_ADDRESS), "r"(PAGE_SHIFT << 2));
507 __asm __volatile("mov %0=psr" : "=r"(psr));
508 __asm __volatile("rsm psr.ic|psr.i");
510 ia64_set_ifa(VM_MAX_ADDRESS);
511 ia64_set_itir(PAGE_SHIFT << 2);
513 __asm __volatile("itr.d dtr[%0]=%1" :: "r"(3), "r"(pte));
515 __asm __volatile("itr.i itr[%0]=%1" :: "r"(3), "r"(pte));
516 __asm __volatile("mov psr.l=%0" :: "r" (psr));
519 /* Expose the mapping to userland in ar.k5 */
520 ia64_set_k5(VM_MAX_ADDRESS);
524 calculate_frequencies(void)
526 struct ia64_sal_result sal;
527 struct ia64_pal_result pal;
529 sal = ia64_sal_entry(SAL_FREQ_BASE, 0, 0, 0, 0, 0, 0, 0);
530 pal = ia64_call_pal_static(PAL_FREQ_RATIOS, 0, 0, 0);
532 if (sal.sal_status == 0 && pal.pal_status == 0) {
534 printf("Platform clock frequency %ld Hz\n",
536 printf("Processor ratio %ld/%ld, Bus ratio %ld/%ld, "
537 "ITC ratio %ld/%ld\n",
538 pal.pal_result[0] >> 32,
539 pal.pal_result[0] & ((1L << 32) - 1),
540 pal.pal_result[1] >> 32,
541 pal.pal_result[1] & ((1L << 32) - 1),
542 pal.pal_result[2] >> 32,
543 pal.pal_result[2] & ((1L << 32) - 1));
545 processor_frequency =
546 sal.sal_result[0] * (pal.pal_result[0] >> 32)
547 / (pal.pal_result[0] & ((1L << 32) - 1));
549 sal.sal_result[0] * (pal.pal_result[1] >> 32)
550 / (pal.pal_result[1] & ((1L << 32) - 1));
552 sal.sal_result[0] * (pal.pal_result[2] >> 32)
553 / (pal.pal_result[2] & ((1L << 32) - 1));
561 vm_offset_t kernstart, kernend;
562 vm_offset_t kernstartpfn, kernendpfn, pfn0, pfn1;
565 int metadata_missing;
567 /* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
570 * TODO: Disable interrupts, floating point etc.
571 * Maybe flush cache and tlb
573 ia64_set_fpsr(IA64_FPSR_DEFAULT);
576 * TODO: Get critical system information (if possible, from the
577 * information provided by the boot program).
581 * pa_bootinfo is the physical address of the bootinfo block as
582 * passed to us by the loader and set in locore.s.
584 bootinfo = *(struct bootinfo *)(IA64_PHYS_TO_RR7(pa_bootinfo));
586 if (bootinfo.bi_magic != BOOTINFO_MAGIC || bootinfo.bi_version != 1) {
587 bzero(&bootinfo, sizeof(bootinfo));
588 bootinfo.bi_kernend = (vm_offset_t) round_page(_end);
592 * Look for the I/O ports first - we need them for console
595 for (md = efi_md_first(); md != NULL; md = efi_md_next(md)) {
596 switch (md->md_type) {
597 case EFI_MD_TYPE_IOPORT:
598 ia64_port_base = IA64_PHYS_TO_RR6(md->md_phys);
600 case EFI_MD_TYPE_PALCODE:
601 ia64_pal_base = md->md_phys;
606 metadata_missing = 0;
607 if (bootinfo.bi_modulep)
608 preload_metadata = (caddr_t)bootinfo.bi_modulep;
610 metadata_missing = 1;
612 if (envmode == 0 && bootinfo.bi_envp)
613 kern_envp = (caddr_t)bootinfo.bi_envp;
615 kern_envp = static_env;
618 * Look at arguments passed to us and compute boothowto.
620 boothowto = bootinfo.bi_boothowto;
623 * Catch case of boot_verbose set in environment.
625 if ((p = getenv("boot_verbose")) != NULL) {
626 if (strcmp(p, "yes") == 0 || strcmp(p, "YES") == 0) {
627 boothowto |= RB_VERBOSE;
632 if (boothowto & RB_VERBOSE)
636 * Setup the PCPU data for the bootstrap processor. It is needed
637 * by printf(). Also, since printf() has critical sections, we
638 * need to initialize at least pc_curthread.
641 ia64_set_k4((u_int64_t)pcpup);
642 pcpu_init(pcpup, 0, sizeof(pcpu0));
643 PCPU_SET(curthread, &thread0);
646 * Initialize the console before we print anything out.
650 /* OUTPUT NOW ALLOWED */
652 if (ia64_pal_base != 0) {
653 ia64_pal_base &= ~IA64_ID_PAGE_MASK;
655 * We use a TR to map the first 256M of memory - this might
656 * cover the palcode too.
658 if (ia64_pal_base == 0)
659 printf("PAL code mapped by the kernel's TR\n");
661 printf("PAL code not found\n");
664 * Wire things up so we can call the firmware.
667 efi_boot_minimal(bootinfo.bi_systab);
669 calculate_frequencies();
672 * Find the beginning and end of the kernel.
674 kernstart = trunc_page(kernel_text);
676 ksym_start = bootinfo.bi_symtab;
677 ksym_end = bootinfo.bi_esymtab;
678 kernend = (vm_offset_t)round_page(ksym_end);
680 kernend = (vm_offset_t)round_page(_end);
683 /* But if the bootstrap tells us otherwise, believe it! */
684 if (bootinfo.bi_kernend)
685 kernend = round_page(bootinfo.bi_kernend);
686 if (metadata_missing)
687 printf("WARNING: loader(8) metadata is missing!\n");
689 /* Get FPSWA interface */
690 fpswa_iface = (bootinfo.bi_fpswa == 0) ? NULL :
691 (struct fpswa_iface *)IA64_PHYS_TO_RR7(bootinfo.bi_fpswa);
693 /* Init basic tunables, including hz */
696 p = getenv("kernelname");
698 strncpy(kernelname, p, sizeof(kernelname) - 1);
702 kernstartpfn = atop(IA64_RR_MASK(kernstart));
703 kernendpfn = atop(IA64_RR_MASK(kernend));
706 * Size the memory regions and load phys_avail[] with the results.
710 * Find out how much memory is available, by looking at
711 * the memory descriptors.
715 printf("Memory descriptor count: %d\n", mdcount);
719 for (md = efi_md_first(); md != NULL; md = efi_md_next(md)) {
721 printf("MD %p: type %d pa 0x%lx cnt 0x%lx\n", md,
722 md->md_type, md->md_phys, md->md_pages);
725 pfn0 = ia64_btop(round_page(md->md_phys));
726 pfn1 = ia64_btop(trunc_page(md->md_phys + md->md_pages * 4096));
730 if (md->md_type != EFI_MD_TYPE_FREE)
734 * We have a memory descriptor that describes conventional
735 * memory that is for general use. We must determine if the
736 * loader has put the kernel in this region.
738 physmem += (pfn1 - pfn0);
739 if (pfn0 <= kernendpfn && kernstartpfn <= pfn1) {
741 * Must compute the location of the kernel
742 * within the segment.
745 printf("Descriptor %p contains kernel\n", mp);
747 if (pfn0 < kernstartpfn) {
749 * There is a chunk before the kernel.
752 printf("Loading chunk before kernel: "
753 "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
755 phys_avail[phys_avail_cnt] = ia64_ptob(pfn0);
756 phys_avail[phys_avail_cnt+1] = ia64_ptob(kernstartpfn);
759 if (kernendpfn < pfn1) {
761 * There is a chunk after the kernel.
764 printf("Loading chunk after kernel: "
765 "0x%lx / 0x%lx\n", kernendpfn, pfn1);
767 phys_avail[phys_avail_cnt] = ia64_ptob(kernendpfn);
768 phys_avail[phys_avail_cnt+1] = ia64_ptob(pfn1);
773 * Just load this cluster as one chunk.
776 printf("Loading descriptor %d: 0x%lx / 0x%lx\n", i,
779 phys_avail[phys_avail_cnt] = ia64_ptob(pfn0);
780 phys_avail[phys_avail_cnt+1] = ia64_ptob(pfn1);
785 phys_avail[phys_avail_cnt] = 0;
788 init_param2(physmem);
791 * Initialize error message buffer (at end of core).
793 msgbufp = (struct msgbuf *)pmap_steal_memory(MSGBUF_SIZE);
794 msgbufinit(msgbufp, MSGBUF_SIZE);
796 proc_linkup0(&proc0, &thread0);
798 * Init mapping for kernel stack for proc 0
800 proc0kstack = (vm_offset_t)kstack;
801 thread0.td_kstack = proc0kstack;
802 thread0.td_kstack_pages = KSTACK_PAGES;
807 * Initialize the rest of proc 0's PCB.
809 * Set the kernel sp, reserving space for an (empty) trapframe,
810 * and make proc0's trapframe pointer point to it for sanity.
811 * Initialise proc0's backing store to start after u area.
813 cpu_thread_alloc(&thread0);
814 thread0.td_frame->tf_flags = FRAME_SYSCALL;
815 thread0.td_pcb->pcb_special.sp =
816 (u_int64_t)thread0.td_frame - 16;
817 thread0.td_pcb->pcb_special.bspstore = thread0.td_kstack;
820 * Initialize the virtual memory system.
825 * Initialize debuggers, and break into them if appropriate.
830 if (boothowto & RB_KDB)
831 kdb_enter_why(KDB_WHY_BOOTFLAGS,
832 "Boot flags requested debugger\n");
839 * Save our current context so that we have a known (maybe even
840 * sane) context as the initial context for new threads that are
841 * forked from us. If any of those threads (including thread0)
842 * does something wrong, we may be lucky and return here where
843 * we're ready for them with a nice panic.
845 if (!savectx(thread0.td_pcb))
848 /* We should not get here. */
849 panic("ia64_init: Whooaa there!");
854 ia64_ioport_address(u_int port)
858 addr = (port > 0xffff) ? IA64_PHYS_TO_RR6((uint64_t)port) :
859 ia64_port_base | ((port & 0xfffc) << 10) | (port & 0xFFF);
860 return ((__volatile void *)addr);
867 return (bootinfo.bi_hcdp);
871 bzero(void *buf, size_t len)
875 while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
879 while (len >= sizeof(u_long) * 8) {
881 *((u_long*) p + 1) = 0;
882 *((u_long*) p + 2) = 0;
883 *((u_long*) p + 3) = 0;
884 len -= sizeof(u_long) * 8;
885 *((u_long*) p + 4) = 0;
886 *((u_long*) p + 5) = 0;
887 *((u_long*) p + 6) = 0;
888 *((u_long*) p + 7) = 0;
889 p += sizeof(u_long) * 8;
891 while (len >= sizeof(u_long)) {
893 len -= sizeof(u_long);
905 u_int64_t start, end, now;
907 start = ia64_get_itc();
908 end = start + (itc_frequency * n) / 1000000;
909 /* printf("DELAY from 0x%lx to 0x%lx\n", start, end); */
911 now = ia64_get_itc();
912 } while (now < end || (now > start && end < start));
916 * Send an interrupt (signal) to a process.
919 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
923 struct trapframe *tf;
925 struct sigframe sf, *sfp;
933 PROC_LOCK_ASSERT(p, MA_OWNED);
934 sig = ksi->ksi_signo;
935 code = ksi->ksi_code;
937 mtx_assert(&psp->ps_mtx, MA_OWNED);
939 sp = tf->tf_special.sp;
940 oonstack = sigonstack(sp);
943 /* save user context */
944 bzero(&sf, sizeof(struct sigframe));
945 sf.sf_uc.uc_sigmask = *mask;
946 sf.sf_uc.uc_stack = td->td_sigstk;
947 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
948 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
951 * Allocate and validate space for the signal handler
952 * context. Note that if the stack is in P0 space, the
953 * call to grow() is a nop, and the useracc() check
954 * will fail if the process has not already allocated
955 * the space with a `brk'.
957 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
958 SIGISMEMBER(psp->ps_sigonstack, sig)) {
959 sbs = (u_int64_t)td->td_sigstk.ss_sp;
960 sbs = (sbs + 15) & ~15;
961 sfp = (struct sigframe *)(sbs + td->td_sigstk.ss_size);
962 #if defined(COMPAT_43)
963 td->td_sigstk.ss_flags |= SS_ONSTACK;
966 sfp = (struct sigframe *)sp;
967 sfp = (struct sigframe *)((u_int64_t)(sfp - 1) & ~15);
969 /* Fill in the siginfo structure for POSIX handlers. */
970 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
971 sf.sf_si = ksi->ksi_info;
972 sf.sf_si.si_signo = sig;
974 * XXX this shouldn't be here after code in trap.c
977 sf.sf_si.si_addr = (void*)tf->tf_special.ifa;
978 code = (u_int64_t)&sfp->sf_si;
981 mtx_unlock(&psp->ps_mtx);
984 get_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
986 /* Copy the frame out to userland. */
987 if (copyout(&sf, sfp, sizeof(sf)) != 0) {
989 * Process has trashed its stack; give it an illegal
990 * instruction to halt it in its tracks.
997 if ((tf->tf_flags & FRAME_SYSCALL) == 0) {
998 tf->tf_special.psr &= ~IA64_PSR_RI;
999 tf->tf_special.iip = ia64_get_k5() +
1000 ((uint64_t)break_sigtramp - (uint64_t)ia64_gateway_page);
1002 tf->tf_special.iip = ia64_get_k5() +
1003 ((uint64_t)epc_sigtramp - (uint64_t)ia64_gateway_page);
1006 * Setup the trapframe to return to the signal trampoline. We pass
1007 * information to the trampoline in the following registers:
1009 * gp new backing store or NULL
1011 * r9 signal code or siginfo pointer
1012 * r10 signal handler (function descriptor)
1014 tf->tf_special.sp = (u_int64_t)sfp - 16;
1015 tf->tf_special.gp = sbs;
1016 tf->tf_special.bspstore = sf.sf_uc.uc_mcontext.mc_special.bspstore;
1017 tf->tf_special.ndirty = 0;
1018 tf->tf_special.rnat = sf.sf_uc.uc_mcontext.mc_special.rnat;
1019 tf->tf_scratch.gr8 = sig;
1020 tf->tf_scratch.gr9 = code;
1021 tf->tf_scratch.gr10 = (u_int64_t)catcher;
1024 mtx_lock(&psp->ps_mtx);
1028 * System call to cleanup state after a signal
1029 * has been taken. Reset signal mask and
1030 * stack state from context left by sendsig (above).
1031 * Return to previous pc and psl as specified by
1032 * context left by sendsig. Check carefully to
1033 * make sure that the user has not modified the
1034 * state to gain improper privileges.
1039 sigreturn(struct thread *td,
1040 struct sigreturn_args /* {
1041 ucontext_t *sigcntxp;
1045 struct trapframe *tf;
1054 * Fetch the entire context structure at once for speed.
1055 * We don't use a normal argument to simplify RSE handling.
1057 if (copyin(uap->sigcntxp, (caddr_t)&uc, sizeof(uc)))
1060 set_mcontext(td, &uc.uc_mcontext);
1063 #if defined(COMPAT_43)
1064 if (sigonstack(tf->tf_special.sp))
1065 td->td_sigstk.ss_flags |= SS_ONSTACK;
1067 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
1069 td->td_sigmask = uc.uc_sigmask;
1070 SIG_CANTMASK(td->td_sigmask);
1074 return (EJUSTRETURN);
1077 #ifdef COMPAT_FREEBSD4
1079 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
1082 return sigreturn(td, (struct sigreturn_args *)uap);
1087 * Construct a PCB from a trapframe. This is called from kdb_trap() where
1088 * we want to start a backtrace from the function that caused us to enter
1089 * the debugger. We have the context in the trapframe, but base the trace
1090 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
1091 * enough for a backtrace.
1094 makectx(struct trapframe *tf, struct pcb *pcb)
1097 pcb->pcb_special = tf->tf_special;
1098 pcb->pcb_special.__spare = ~0UL; /* XXX see unwind.c */
1099 save_callee_saved(&pcb->pcb_preserved);
1100 save_callee_saved_fp(&pcb->pcb_preserved_fp);
1104 ia64_flush_dirty(struct thread *td, struct _special *r)
1108 uint64_t bspst, kstk, rnat;
1114 kstk = td->td_kstack + (r->bspstore & 0x1ffUL);
1115 if (td == curthread) {
1116 __asm __volatile("mov ar.rsc=0;;");
1117 __asm __volatile("mov %0=ar.bspstore" : "=r"(bspst));
1118 /* Make sure we have all the user registers written out. */
1119 if (bspst - kstk < r->ndirty) {
1120 __asm __volatile("flushrs;;");
1121 __asm __volatile("mov %0=ar.bspstore" : "=r"(bspst));
1123 __asm __volatile("mov %0=ar.rnat;;" : "=r"(rnat));
1124 __asm __volatile("mov ar.rsc=3");
1125 error = copyout((void*)kstk, (void*)r->bspstore, r->ndirty);
1127 r->rnat = (bspst > kstk && (bspst & 0x1ffL) < (kstk & 0x1ffL))
1128 ? *(uint64_t*)(kstk | 0x1f8L) : rnat;
1130 locked = PROC_LOCKED(td->td_proc);
1133 iov.iov_base = (void*)(uintptr_t)kstk;
1134 iov.iov_len = r->ndirty;
1137 uio.uio_offset = r->bspstore;
1138 uio.uio_resid = r->ndirty;
1139 uio.uio_segflg = UIO_SYSSPACE;
1140 uio.uio_rw = UIO_WRITE;
1142 error = proc_rwmem(td->td_proc, &uio);
1144 * XXX proc_rwmem() doesn't currently return ENOSPC,
1145 * so I think it can bogusly return 0. Neither do
1146 * we allow short writes.
1148 if (uio.uio_resid != 0 && error == 0)
1154 r->bspstore += r->ndirty;
1160 get_mcontext(struct thread *td, mcontext_t *mc, int flags)
1162 struct trapframe *tf;
1166 bzero(mc, sizeof(*mc));
1167 mc->mc_special = tf->tf_special;
1168 error = ia64_flush_dirty(td, &mc->mc_special);
1169 if (tf->tf_flags & FRAME_SYSCALL) {
1170 mc->mc_flags |= _MC_FLAGS_SYSCALL_CONTEXT;
1171 mc->mc_scratch = tf->tf_scratch;
1172 if (flags & GET_MC_CLEAR_RET) {
1173 mc->mc_scratch.gr8 = 0;
1174 mc->mc_scratch.gr9 = 0;
1175 mc->mc_scratch.gr10 = 0;
1176 mc->mc_scratch.gr11 = 0;
1179 mc->mc_flags |= _MC_FLAGS_ASYNC_CONTEXT;
1180 mc->mc_scratch = tf->tf_scratch;
1181 mc->mc_scratch_fp = tf->tf_scratch_fp;
1183 * XXX If the thread never used the high FP registers, we
1184 * probably shouldn't waste time saving them.
1186 ia64_highfp_save(td);
1187 mc->mc_flags |= _MC_FLAGS_HIGHFP_VALID;
1188 mc->mc_high_fp = td->td_pcb->pcb_high_fp;
1190 save_callee_saved(&mc->mc_preserved);
1191 save_callee_saved_fp(&mc->mc_preserved_fp);
1196 set_mcontext(struct thread *td, const mcontext_t *mc)
1199 struct trapframe *tf;
1204 KASSERT((tf->tf_special.ndirty & ~PAGE_MASK) == 0,
1205 ("Whoa there! We have more than 8KB of dirty registers!"));
1209 * Only copy the user mask and the restart instruction bit from
1212 psrmask = IA64_PSR_BE | IA64_PSR_UP | IA64_PSR_AC | IA64_PSR_MFL |
1213 IA64_PSR_MFH | IA64_PSR_RI;
1214 s.psr = (tf->tf_special.psr & ~psrmask) | (s.psr & psrmask);
1215 /* We don't have any dirty registers of the new context. */
1217 if (mc->mc_flags & _MC_FLAGS_ASYNC_CONTEXT) {
1219 * We can get an async context passed to us while we
1220 * entered the kernel through a syscall: sigreturn(2)
1221 * and kse_switchin(2) both take contexts that could
1222 * previously be the result of a trap or interrupt.
1223 * Hence, we cannot assert that the trapframe is not
1224 * a syscall frame, but we can assert that it's at
1225 * least an expected syscall.
1227 if (tf->tf_flags & FRAME_SYSCALL) {
1228 KASSERT(tf->tf_scratch.gr15 == SYS_sigreturn ||
1229 tf->tf_scratch.gr15 == SYS_kse_switchin, ("foo"));
1230 tf->tf_flags &= ~FRAME_SYSCALL;
1232 tf->tf_scratch = mc->mc_scratch;
1233 tf->tf_scratch_fp = mc->mc_scratch_fp;
1234 if (mc->mc_flags & _MC_FLAGS_HIGHFP_VALID)
1235 td->td_pcb->pcb_high_fp = mc->mc_high_fp;
1237 KASSERT((tf->tf_flags & FRAME_SYSCALL) != 0, ("foo"));
1238 if ((mc->mc_flags & _MC_FLAGS_SYSCALL_CONTEXT) == 0) {
1239 s.cfm = tf->tf_special.cfm;
1240 s.iip = tf->tf_special.iip;
1241 tf->tf_scratch.gr15 = 0; /* Clear syscall nr. */
1243 tf->tf_scratch = mc->mc_scratch;
1246 restore_callee_saved(&mc->mc_preserved);
1247 restore_callee_saved_fp(&mc->mc_preserved_fp);
1249 if (mc->mc_flags & _MC_FLAGS_KSE_SET_MBOX)
1250 suword((caddr_t)mc->mc_special.ifa, mc->mc_special.isr);
1256 * Clear registers on exec.
1259 exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
1261 struct trapframe *tf;
1262 uint64_t *ksttop, *kst;
1265 ksttop = (uint64_t*)(td->td_kstack + tf->tf_special.ndirty +
1266 (tf->tf_special.bspstore & 0x1ffUL));
1269 * We can ignore up to 8KB of dirty registers by masking off the
1270 * lower 13 bits in exception_restore() or epc_syscall(). This
1271 * should be enough for a couple of years, but if there are more
1272 * than 8KB of dirty registers, we lose track of the bottom of
1273 * the kernel stack. The solution is to copy the active part of
1274 * the kernel stack down 1 page (or 2, but not more than that)
1275 * so that we always have less than 8KB of dirty registers.
1277 KASSERT((tf->tf_special.ndirty & ~PAGE_MASK) == 0,
1278 ("Whoa there! We have more than 8KB of dirty registers!"));
1280 bzero(&tf->tf_special, sizeof(tf->tf_special));
1281 if ((tf->tf_flags & FRAME_SYSCALL) == 0) { /* break syscalls. */
1282 bzero(&tf->tf_scratch, sizeof(tf->tf_scratch));
1283 bzero(&tf->tf_scratch_fp, sizeof(tf->tf_scratch_fp));
1284 tf->tf_special.cfm = (1UL<<63) | (3UL<<7) | 3UL;
1285 tf->tf_special.bspstore = IA64_BACKINGSTORE;
1287 * Copy the arguments onto the kernel register stack so that
1288 * they get loaded by the loadrs instruction. Skip over the
1289 * NaT collection points.
1292 if (((uintptr_t)kst & 0x1ff) == 0x1f8)
1295 if (((uintptr_t)kst & 0x1ff) == 0x1f8)
1297 *kst-- = ps_strings;
1298 if (((uintptr_t)kst & 0x1ff) == 0x1f8)
1301 tf->tf_special.ndirty = (ksttop - kst) << 3;
1302 } else { /* epc syscalls (default). */
1303 tf->tf_special.cfm = (3UL<<62) | (3UL<<7) | 3UL;
1304 tf->tf_special.bspstore = IA64_BACKINGSTORE + 24;
1306 * Write values for out0, out1 and out2 to the user's backing
1307 * store and arrange for them to be restored into the user's
1308 * initial register frame.
1309 * Assumes that (bspstore & 0x1f8) < 0x1e0.
1311 suword((caddr_t)tf->tf_special.bspstore - 24, stack);
1312 suword((caddr_t)tf->tf_special.bspstore - 16, ps_strings);
1313 suword((caddr_t)tf->tf_special.bspstore - 8, 0);
1316 tf->tf_special.iip = entry;
1317 tf->tf_special.sp = (stack & ~15) - 16;
1318 tf->tf_special.rsc = 0xf;
1319 tf->tf_special.fpsr = IA64_FPSR_DEFAULT;
1320 tf->tf_special.psr = IA64_PSR_IC | IA64_PSR_I | IA64_PSR_IT |
1321 IA64_PSR_DT | IA64_PSR_RT | IA64_PSR_DFH | IA64_PSR_BN |
1326 ptrace_set_pc(struct thread *td, unsigned long addr)
1330 switch (addr & 0xFUL) {
1332 slot = IA64_PSR_RI_0;
1335 /* XXX we need to deal with MLX bundles here */
1336 slot = IA64_PSR_RI_1;
1339 slot = IA64_PSR_RI_2;
1345 td->td_frame->tf_special.iip = addr & ~0x0FULL;
1346 td->td_frame->tf_special.psr =
1347 (td->td_frame->tf_special.psr & ~IA64_PSR_RI) | slot;
1352 ptrace_single_step(struct thread *td)
1354 struct trapframe *tf;
1357 * There's no way to set single stepping when we're leaving the
1358 * kernel through the EPC syscall path. The way we solve this is
1359 * by enabling the lower-privilege trap so that we re-enter the
1360 * kernel as soon as the privilege level changes. See trap.c for
1361 * how we proceed from there.
1364 if (tf->tf_flags & FRAME_SYSCALL)
1365 tf->tf_special.psr |= IA64_PSR_LP;
1367 tf->tf_special.psr |= IA64_PSR_SS;
1372 ptrace_clear_single_step(struct thread *td)
1374 struct trapframe *tf;
1377 * Clear any and all status bits we may use to implement single
1381 tf->tf_special.psr &= ~IA64_PSR_SS;
1382 tf->tf_special.psr &= ~IA64_PSR_LP;
1383 tf->tf_special.psr &= ~IA64_PSR_TB;
1388 fill_regs(struct thread *td, struct reg *regs)
1390 struct trapframe *tf;
1393 regs->r_special = tf->tf_special;
1394 regs->r_scratch = tf->tf_scratch;
1395 save_callee_saved(®s->r_preserved);
1400 set_regs(struct thread *td, struct reg *regs)
1402 struct trapframe *tf;
1406 error = ia64_flush_dirty(td, &tf->tf_special);
1408 tf->tf_special = regs->r_special;
1409 tf->tf_special.bspstore += tf->tf_special.ndirty;
1410 tf->tf_special.ndirty = 0;
1411 tf->tf_scratch = regs->r_scratch;
1412 restore_callee_saved(®s->r_preserved);
1418 fill_dbregs(struct thread *td, struct dbreg *dbregs)
1425 set_dbregs(struct thread *td, struct dbreg *dbregs)
1432 fill_fpregs(struct thread *td, struct fpreg *fpregs)
1434 struct trapframe *frame = td->td_frame;
1435 struct pcb *pcb = td->td_pcb;
1437 /* Save the high FP registers. */
1438 ia64_highfp_save(td);
1440 fpregs->fpr_scratch = frame->tf_scratch_fp;
1441 save_callee_saved_fp(&fpregs->fpr_preserved);
1442 fpregs->fpr_high = pcb->pcb_high_fp;
1447 set_fpregs(struct thread *td, struct fpreg *fpregs)
1449 struct trapframe *frame = td->td_frame;
1450 struct pcb *pcb = td->td_pcb;
1452 /* Throw away the high FP registers (should be redundant). */
1453 ia64_highfp_drop(td);
1455 frame->tf_scratch_fp = fpregs->fpr_scratch;
1456 restore_callee_saved_fp(&fpregs->fpr_preserved);
1457 pcb->pcb_high_fp = fpregs->fpr_high;
1462 * High FP register functions.
1466 ia64_highfp_drop(struct thread *td)
1472 mtx_lock_spin(&td->td_md.md_highfp_mtx);
1474 cpu = pcb->pcb_fpcpu;
1476 mtx_unlock_spin(&td->td_md.md_highfp_mtx);
1479 pcb->pcb_fpcpu = NULL;
1480 thr = cpu->pc_fpcurthread;
1481 cpu->pc_fpcurthread = NULL;
1482 mtx_unlock_spin(&td->td_md.md_highfp_mtx);
1484 /* Post-mortem sanity checking. */
1485 KASSERT(thr == td, ("Inconsistent high FP state"));
1490 ia64_highfp_save(struct thread *td)
1496 /* Don't save if the high FP registers weren't modified. */
1497 if ((td->td_frame->tf_special.psr & IA64_PSR_MFH) == 0)
1498 return (ia64_highfp_drop(td));
1500 mtx_lock_spin(&td->td_md.md_highfp_mtx);
1502 cpu = pcb->pcb_fpcpu;
1504 mtx_unlock_spin(&td->td_md.md_highfp_mtx);
1508 if (td == curthread)
1511 mtx_unlock_spin(&td->td_md.md_highfp_mtx);
1512 ipi_send(cpu, IPI_HIGH_FP);
1513 if (td == curthread)
1515 while (pcb->pcb_fpcpu == cpu)
1519 save_high_fp(&pcb->pcb_high_fp);
1520 if (td == curthread)
1524 save_high_fp(&pcb->pcb_high_fp);
1526 pcb->pcb_fpcpu = NULL;
1527 thr = cpu->pc_fpcurthread;
1528 cpu->pc_fpcurthread = NULL;
1529 mtx_unlock_spin(&td->td_md.md_highfp_mtx);
1531 /* Post-mortem sanity cxhecking. */
1532 KASSERT(thr == td, ("Inconsistent high FP state"));
1537 ia64_invalidate_icache(vm_offset_t va, vm_offset_t sz)
1541 if (!ia64_inval_icache_needed)
1546 __asm __volatile("fc.i %0" :: "r"(va));
1552 sysbeep(int pitch, int period)