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"
35 #include "opt_sched.h"
37 #include <sys/param.h>
39 #include <sys/systm.h>
45 #include <sys/eventhandler.h>
47 #include <sys/imgact.h>
49 #include <sys/kernel.h>
50 #include <sys/linker.h>
52 #include <sys/malloc.h>
54 #include <sys/msgbuf.h>
56 #include <sys/ptrace.h>
57 #include <sys/random.h>
58 #include <sys/reboot.h>
59 #include <sys/sched.h>
60 #include <sys/signalvar.h>
61 #include <sys/syscall.h>
62 #include <sys/sysctl.h>
63 #include <sys/sysproto.h>
64 #include <sys/ucontext.h>
67 #include <sys/vmmeter.h>
68 #include <sys/vnode.h>
72 #include <net/netisr.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_kern.h>
77 #include <vm/vm_page.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_pager.h>
82 #include <machine/bootinfo.h>
83 #include <machine/clock.h>
84 #include <machine/cpu.h>
85 #include <machine/efi.h>
86 #include <machine/elf.h>
87 #include <machine/fpu.h>
88 #include <machine/mca.h>
89 #include <machine/md_var.h>
90 #include <machine/mutex.h>
91 #include <machine/pal.h>
92 #include <machine/pcb.h>
93 #include <machine/reg.h>
94 #include <machine/sal.h>
95 #include <machine/sigframe.h>
97 #include <machine/smp.h>
99 #include <machine/unwind.h>
100 #include <machine/vmparam.h>
102 #include <i386/include/specialreg.h>
104 u_int64_t processor_frequency;
105 u_int64_t bus_frequency;
106 u_int64_t itc_frequency;
109 u_int64_t pa_bootinfo;
110 struct bootinfo bootinfo;
114 extern u_int64_t kernel_text[], _end[];
116 extern u_int64_t ia64_gateway_page[];
117 extern u_int64_t break_sigtramp[];
118 extern u_int64_t epc_sigtramp[];
120 struct fpswa_iface *fpswa_iface;
122 u_int64_t ia64_pal_base;
123 u_int64_t ia64_port_base;
125 static int ia64_sync_icache_needed;
127 char machine[] = MACHINE;
128 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
130 static char cpu_model[64];
131 SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD, cpu_model, 0,
132 "The CPU model name");
134 static char cpu_family[64];
135 SYSCTL_STRING(_hw, OID_AUTO, family, CTLFLAG_RD, cpu_family, 0,
136 "The CPU family name");
139 extern vm_offset_t ksym_start, ksym_end;
142 static void cpu_startup(void *);
143 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
145 struct msgbuf *msgbufp = NULL;
147 /* Other subsystems (e.g., ACPI) can hook this later. */
148 void (*cpu_idle_hook)(void) = NULL;
153 #define PHYSMAP_SIZE (2 * VM_PHYSSEG_MAX)
155 vm_paddr_t phys_avail[PHYSMAP_SIZE + 2];
157 /* must be 2 less so 0 0 can signal end of chunks */
158 #define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2)
160 struct kva_md_info kmi;
163 #define Ghz (1000L*Mhz)
169 char *family_name, *model_name;
170 u_int64_t features, tmp;
171 int number, revision, model, family, archrev;
174 * Assumes little-endian.
176 *(u_int64_t *) &vendor[0] = ia64_get_cpuid(0);
177 *(u_int64_t *) &vendor[8] = ia64_get_cpuid(1);
180 tmp = ia64_get_cpuid(3);
181 number = (tmp >> 0) & 0xff;
182 revision = (tmp >> 8) & 0xff;
183 model = (tmp >> 16) & 0xff;
184 family = (tmp >> 24) & 0xff;
185 archrev = (tmp >> 32) & 0xff;
187 family_name = model_name = "unknown";
190 family_name = "Itanium";
191 model_name = "Merced";
194 family_name = "Itanium 2";
197 model_name = "McKinley";
201 * Deerfield is a low-voltage variant based on the
202 * Madison core. We need circumstantial evidence
203 * (i.e. the clock frequency) to identify those.
204 * Allow for roughly 1% error margin.
206 tmp = processor_frequency >> 7;
207 if ((processor_frequency - tmp) < 1*Ghz &&
208 (processor_frequency + tmp) >= 1*Ghz)
209 model_name = "Deerfield";
211 model_name = "Madison";
214 model_name = "Madison II";
219 ia64_sync_icache_needed = 1;
221 family_name = "Itanium 2";
224 model_name = "Montecito";
229 snprintf(cpu_family, sizeof(cpu_family), "%s", family_name);
230 snprintf(cpu_model, sizeof(cpu_model), "%s", model_name);
232 features = ia64_get_cpuid(4);
234 printf("CPU: %s (", model_name);
235 if (processor_frequency) {
236 printf("%ld.%02ld-Mhz ",
237 (processor_frequency + 4999) / Mhz,
238 ((processor_frequency + 4999) / (Mhz/100)) % 100);
240 printf("%s)\n", family_name);
241 printf(" Origin = \"%s\" Revision = %d\n", vendor, revision);
242 printf(" Features = 0x%b\n", (u_int32_t) features,
244 "\001LB" /* long branch (brl) instruction. */
245 "\002SD" /* Spontaneous deferral. */
246 "\003AO" /* 16-byte atomic operations (ld, st, cmpxchg). */ );
255 * Good {morning,afternoon,evening,night}.
262 printf("real memory = %ld (%ld MB)\n", ia64_ptob(Maxmem),
263 ia64_ptob(Maxmem) / 1048576);
267 * Display any holes after the first chunk of extended memory.
272 printf("Physical memory chunk(s):\n");
273 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
274 long size1 = phys_avail[indx + 1] - phys_avail[indx];
276 printf("0x%08lx - 0x%08lx, %ld bytes (%ld pages)\n",
277 phys_avail[indx], phys_avail[indx + 1] - 1, size1,
278 size1 >> PAGE_SHIFT);
282 vm_ksubmap_init(&kmi);
284 printf("avail memory = %ld (%ld MB)\n", ptoa(cnt.v_free_count),
285 ptoa(cnt.v_free_count) / 1048576);
287 if (fpswa_iface == NULL)
288 printf("Warning: no FPSWA package supplied\n");
290 printf("FPSWA Revision = 0x%lx, Entry = %p\n",
291 (long)fpswa_iface->if_rev, (void *)fpswa_iface->if_fpswa);
294 * Set up buffers, so they can be used to read disk labels.
297 vm_pager_bufferinit();
300 * Traverse the MADT to discover IOSAPIC and Local SAPIC
315 cpu_flush_dcache(void *ptr, size_t len)
319 va = (uintptr_t)ptr & ~31;
320 lim = (uintptr_t)ptr + len;
329 /* Get current clock frequency for the given cpu id. */
331 cpu_est_clockrate(int cpu_id, uint64_t *rate)
334 if (pcpu_find(cpu_id) == NULL || rate == NULL)
336 *rate = processor_frequency;
350 struct ia64_pal_result res;
352 if (cpu_idle_hook != NULL)
355 res = ia64_call_pal_static(PAL_HALT_LIGHT, 0, 0, 0);
359 cpu_idle_wakeup(int cpu)
373 cpu_switch(struct thread *old, struct thread *new, struct mtx *mtx)
375 struct pcb *oldpcb, *newpcb;
377 oldpcb = old->td_pcb;
379 ia32_savectx(oldpcb);
381 if (PCPU_GET(fpcurthread) == old)
382 old->td_frame->tf_special.psr |= IA64_PSR_DFH;
383 if (!savectx(oldpcb)) {
385 #if defined(SCHED_ULE) && defined(SMP)
386 /* td_lock is volatile */
387 while (new->td_lock == &blocked_lock)
390 newpcb = new->td_pcb;
391 oldpcb->pcb_current_pmap =
392 pmap_switch(newpcb->pcb_current_pmap);
393 PCPU_SET(curthread, new);
395 ia32_restorectx(newpcb);
397 if (PCPU_GET(fpcurthread) == new)
398 new->td_frame->tf_special.psr &= ~IA64_PSR_DFH;
400 /* We should not get here. */
401 panic("cpu_switch: restorectx() returned");
407 cpu_throw(struct thread *old __unused, struct thread *new)
411 newpcb = new->td_pcb;
412 (void)pmap_switch(newpcb->pcb_current_pmap);
413 PCPU_SET(curthread, new);
415 ia32_restorectx(newpcb);
418 /* We should not get here. */
419 panic("cpu_throw: restorectx() returned");
424 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
428 * Set pc_acpi_id to "uninitialized".
429 * See sys/dev/acpica/acpi_cpu.c
431 pcpu->pc_acpi_id = 0xffffffff;
440 if (td->td_md.md_spinlock_count == 0)
441 td->td_md.md_saved_intr = intr_disable();
442 td->td_md.md_spinlock_count++;
453 td->td_md.md_spinlock_count--;
454 if (td->td_md.md_spinlock_count == 0)
455 intr_restore(td->td_md.md_saved_intr);
459 map_vhpt(uintptr_t vhpt)
464 pte = PTE_PRESENT | PTE_MA_WB | PTE_ACCESSED | PTE_DIRTY |
465 PTE_PL_KERN | PTE_AR_RW;
466 pte |= vhpt & PTE_PPN_MASK;
468 __asm __volatile("ptr.d %0,%1" :: "r"(vhpt),
469 "r"(IA64_ID_PAGE_SHIFT<<2));
471 __asm __volatile("mov %0=psr" : "=r"(psr));
472 __asm __volatile("rsm psr.ic|psr.i");
475 ia64_set_itir(IA64_ID_PAGE_SHIFT << 2);
477 __asm __volatile("itr.d dtr[%0]=%1" :: "r"(2), "r"(pte));
478 __asm __volatile("mov psr.l=%0" :: "r" (psr));
488 if (ia64_pal_base == 0)
491 pte = PTE_PRESENT | PTE_MA_WB | PTE_ACCESSED | PTE_DIRTY |
492 PTE_PL_KERN | PTE_AR_RWX;
493 pte |= ia64_pal_base & PTE_PPN_MASK;
495 __asm __volatile("ptr.d %0,%1; ptr.i %0,%1" ::
496 "r"(IA64_PHYS_TO_RR7(ia64_pal_base)), "r"(IA64_ID_PAGE_SHIFT<<2));
498 __asm __volatile("mov %0=psr" : "=r"(psr));
499 __asm __volatile("rsm psr.ic|psr.i");
501 ia64_set_ifa(IA64_PHYS_TO_RR7(ia64_pal_base));
502 ia64_set_itir(IA64_ID_PAGE_SHIFT << 2);
504 __asm __volatile("itr.d dtr[%0]=%1" :: "r"(1), "r"(pte));
506 __asm __volatile("itr.i itr[%0]=%1" :: "r"(1), "r"(pte));
507 __asm __volatile("mov psr.l=%0" :: "r" (psr));
512 map_gateway_page(void)
517 pte = PTE_PRESENT | PTE_MA_WB | PTE_ACCESSED | PTE_DIRTY |
518 PTE_PL_KERN | PTE_AR_X_RX;
519 pte |= (uint64_t)ia64_gateway_page & PTE_PPN_MASK;
521 __asm __volatile("ptr.d %0,%1; ptr.i %0,%1" ::
522 "r"(VM_MAX_ADDRESS), "r"(PAGE_SHIFT << 2));
524 __asm __volatile("mov %0=psr" : "=r"(psr));
525 __asm __volatile("rsm psr.ic|psr.i");
527 ia64_set_ifa(VM_MAX_ADDRESS);
528 ia64_set_itir(PAGE_SHIFT << 2);
530 __asm __volatile("itr.d dtr[%0]=%1" :: "r"(3), "r"(pte));
532 __asm __volatile("itr.i itr[%0]=%1" :: "r"(3), "r"(pte));
533 __asm __volatile("mov psr.l=%0" :: "r" (psr));
536 /* Expose the mapping to userland in ar.k5 */
537 ia64_set_k5(VM_MAX_ADDRESS);
541 calculate_frequencies(void)
543 struct ia64_sal_result sal;
544 struct ia64_pal_result pal;
546 sal = ia64_sal_entry(SAL_FREQ_BASE, 0, 0, 0, 0, 0, 0, 0);
547 pal = ia64_call_pal_static(PAL_FREQ_RATIOS, 0, 0, 0);
549 if (sal.sal_status == 0 && pal.pal_status == 0) {
551 printf("Platform clock frequency %ld Hz\n",
553 printf("Processor ratio %ld/%ld, Bus ratio %ld/%ld, "
554 "ITC ratio %ld/%ld\n",
555 pal.pal_result[0] >> 32,
556 pal.pal_result[0] & ((1L << 32) - 1),
557 pal.pal_result[1] >> 32,
558 pal.pal_result[1] & ((1L << 32) - 1),
559 pal.pal_result[2] >> 32,
560 pal.pal_result[2] & ((1L << 32) - 1));
562 processor_frequency =
563 sal.sal_result[0] * (pal.pal_result[0] >> 32)
564 / (pal.pal_result[0] & ((1L << 32) - 1));
566 sal.sal_result[0] * (pal.pal_result[1] >> 32)
567 / (pal.pal_result[1] & ((1L << 32) - 1));
569 sal.sal_result[0] * (pal.pal_result[2] >> 32)
570 / (pal.pal_result[2] & ((1L << 32) - 1));
574 struct ia64_init_return
577 struct ia64_init_return ret;
579 vm_offset_t kernstart, kernend;
580 vm_offset_t kernstartpfn, kernendpfn, pfn0, pfn1;
583 int metadata_missing;
585 /* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
588 * TODO: Disable interrupts, floating point etc.
589 * Maybe flush cache and tlb
591 ia64_set_fpsr(IA64_FPSR_DEFAULT);
594 * TODO: Get critical system information (if possible, from the
595 * information provided by the boot program).
599 * pa_bootinfo is the physical address of the bootinfo block as
600 * passed to us by the loader and set in locore.s.
602 bootinfo = *(struct bootinfo *)(IA64_PHYS_TO_RR7(pa_bootinfo));
604 if (bootinfo.bi_magic != BOOTINFO_MAGIC || bootinfo.bi_version != 1) {
605 bzero(&bootinfo, sizeof(bootinfo));
606 bootinfo.bi_kernend = (vm_offset_t) round_page(_end);
610 * Look for the I/O ports first - we need them for console
613 for (md = efi_md_first(); md != NULL; md = efi_md_next(md)) {
614 switch (md->md_type) {
615 case EFI_MD_TYPE_IOPORT:
616 ia64_port_base = IA64_PHYS_TO_RR6(md->md_phys);
618 case EFI_MD_TYPE_PALCODE:
619 ia64_pal_base = md->md_phys;
624 metadata_missing = 0;
625 if (bootinfo.bi_modulep)
626 preload_metadata = (caddr_t)bootinfo.bi_modulep;
628 metadata_missing = 1;
630 if (envmode == 0 && bootinfo.bi_envp)
631 kern_envp = (caddr_t)bootinfo.bi_envp;
633 kern_envp = static_env;
636 * Look at arguments passed to us and compute boothowto.
638 boothowto = bootinfo.bi_boothowto;
641 * Catch case of boot_verbose set in environment.
643 if ((p = getenv("boot_verbose")) != NULL) {
644 if (strcmp(p, "yes") == 0 || strcmp(p, "YES") == 0) {
645 boothowto |= RB_VERBOSE;
650 if (boothowto & RB_VERBOSE)
654 * Find the beginning and end of the kernel.
656 kernstart = trunc_page(kernel_text);
658 ksym_start = bootinfo.bi_symtab;
659 ksym_end = bootinfo.bi_esymtab;
660 kernend = (vm_offset_t)round_page(ksym_end);
662 kernend = (vm_offset_t)round_page(_end);
664 /* But if the bootstrap tells us otherwise, believe it! */
665 if (bootinfo.bi_kernend)
666 kernend = round_page(bootinfo.bi_kernend);
669 * Setup the PCPU data for the bootstrap processor. It is needed
670 * by printf(). Also, since printf() has critical sections, we
671 * need to initialize at least pc_curthread.
674 ia64_set_k4((u_int64_t)pcpup);
675 pcpu_init(pcpup, 0, sizeof(pcpu0));
676 dpcpu_init((void *)kernend, 0);
677 kernend += DPCPU_SIZE;
678 PCPU_SET(curthread, &thread0);
681 * Initialize the console before we print anything out.
685 /* OUTPUT NOW ALLOWED */
687 if (ia64_pal_base != 0) {
688 ia64_pal_base &= ~IA64_ID_PAGE_MASK;
690 * We use a TR to map the first 256M of memory - this might
691 * cover the palcode too.
693 if (ia64_pal_base == 0)
694 printf("PAL code mapped by the kernel's TR\n");
696 printf("PAL code not found\n");
699 * Wire things up so we can call the firmware.
702 efi_boot_minimal(bootinfo.bi_systab);
704 calculate_frequencies();
706 if (metadata_missing)
707 printf("WARNING: loader(8) metadata is missing!\n");
709 /* Get FPSWA interface */
710 fpswa_iface = (bootinfo.bi_fpswa == 0) ? NULL :
711 (struct fpswa_iface *)IA64_PHYS_TO_RR7(bootinfo.bi_fpswa);
713 /* Init basic tunables, including hz */
716 p = getenv("kernelname");
718 strncpy(kernelname, p, sizeof(kernelname) - 1);
722 kernstartpfn = atop(IA64_RR_MASK(kernstart));
723 kernendpfn = atop(IA64_RR_MASK(kernend));
726 * Size the memory regions and load phys_avail[] with the results.
730 * Find out how much memory is available, by looking at
731 * the memory descriptors.
735 printf("Memory descriptor count: %d\n", mdcount);
739 for (md = efi_md_first(); md != NULL; md = efi_md_next(md)) {
741 printf("MD %p: type %d pa 0x%lx cnt 0x%lx\n", md,
742 md->md_type, md->md_phys, md->md_pages);
745 pfn0 = ia64_btop(round_page(md->md_phys));
746 pfn1 = ia64_btop(trunc_page(md->md_phys + md->md_pages * 4096));
750 if (md->md_type != EFI_MD_TYPE_FREE)
754 * We have a memory descriptor that describes conventional
755 * memory that is for general use. We must determine if the
756 * loader has put the kernel in this region.
758 physmem += (pfn1 - pfn0);
759 if (pfn0 <= kernendpfn && kernstartpfn <= pfn1) {
761 * Must compute the location of the kernel
762 * within the segment.
765 printf("Descriptor %p contains kernel\n", mp);
767 if (pfn0 < kernstartpfn) {
769 * There is a chunk before the kernel.
772 printf("Loading chunk before kernel: "
773 "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
775 phys_avail[phys_avail_cnt] = ia64_ptob(pfn0);
776 phys_avail[phys_avail_cnt+1] = ia64_ptob(kernstartpfn);
779 if (kernendpfn < pfn1) {
781 * There is a chunk after the kernel.
784 printf("Loading chunk after kernel: "
785 "0x%lx / 0x%lx\n", kernendpfn, pfn1);
787 phys_avail[phys_avail_cnt] = ia64_ptob(kernendpfn);
788 phys_avail[phys_avail_cnt+1] = ia64_ptob(pfn1);
793 * Just load this cluster as one chunk.
796 printf("Loading descriptor %d: 0x%lx / 0x%lx\n", i,
799 phys_avail[phys_avail_cnt] = ia64_ptob(pfn0);
800 phys_avail[phys_avail_cnt+1] = ia64_ptob(pfn1);
805 phys_avail[phys_avail_cnt] = 0;
808 init_param2(physmem);
811 * Initialize error message buffer (at end of core).
813 msgbufp = (struct msgbuf *)pmap_steal_memory(MSGBUF_SIZE);
814 msgbufinit(msgbufp, MSGBUF_SIZE);
816 proc_linkup0(&proc0, &thread0);
818 * Init mapping for kernel stack for proc 0
820 thread0.td_kstack = pmap_steal_memory(KSTACK_PAGES * PAGE_SIZE);
821 thread0.td_kstack_pages = KSTACK_PAGES;
826 * Initialize the rest of proc 0's PCB.
828 * Set the kernel sp, reserving space for an (empty) trapframe,
829 * and make proc0's trapframe pointer point to it for sanity.
830 * Initialise proc0's backing store to start after u area.
832 cpu_thread_alloc(&thread0);
833 thread0.td_frame->tf_flags = FRAME_SYSCALL;
834 thread0.td_pcb->pcb_special.sp =
835 (u_int64_t)thread0.td_frame - 16;
836 thread0.td_pcb->pcb_special.bspstore = thread0.td_kstack;
839 * Initialize the virtual memory system.
844 * Initialize debuggers, and break into them if appropriate.
849 if (boothowto & RB_KDB)
850 kdb_enter(KDB_WHY_BOOTFLAGS,
851 "Boot flags requested debugger\n");
857 ret.bspstore = thread0.td_pcb->pcb_special.bspstore;
858 ret.sp = thread0.td_pcb->pcb_special.sp;
863 ia64_ioport_address(u_int port)
867 addr = (port > 0xffff) ? IA64_PHYS_TO_RR6((uint64_t)port) :
868 ia64_port_base | ((port & 0xfffc) << 10) | (port & 0xFFF);
869 return ((__volatile void *)addr);
876 return (bootinfo.bi_hcdp);
880 bzero(void *buf, size_t len)
884 while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
888 while (len >= sizeof(u_long) * 8) {
890 *((u_long*) p + 1) = 0;
891 *((u_long*) p + 2) = 0;
892 *((u_long*) p + 3) = 0;
893 len -= sizeof(u_long) * 8;
894 *((u_long*) p + 4) = 0;
895 *((u_long*) p + 5) = 0;
896 *((u_long*) p + 6) = 0;
897 *((u_long*) p + 7) = 0;
898 p += sizeof(u_long) * 8;
900 while (len >= sizeof(u_long)) {
902 len -= sizeof(u_long);
914 u_int64_t start, end, now;
918 start = ia64_get_itc();
919 end = start + (itc_frequency * n) / 1000000;
920 /* printf("DELAY from 0x%lx to 0x%lx\n", start, end); */
922 now = ia64_get_itc();
923 } while (now < end || (now > start && end < start));
929 * Send an interrupt (signal) to a process.
932 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
936 struct trapframe *tf;
938 struct sigframe sf, *sfp;
946 PROC_LOCK_ASSERT(p, MA_OWNED);
947 sig = ksi->ksi_signo;
948 code = ksi->ksi_code;
950 mtx_assert(&psp->ps_mtx, MA_OWNED);
952 sp = tf->tf_special.sp;
953 oonstack = sigonstack(sp);
956 /* save user context */
957 bzero(&sf, sizeof(struct sigframe));
958 sf.sf_uc.uc_sigmask = *mask;
959 sf.sf_uc.uc_stack = td->td_sigstk;
960 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
961 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
964 * Allocate and validate space for the signal handler
965 * context. Note that if the stack is in P0 space, the
966 * call to grow() is a nop, and the useracc() check
967 * will fail if the process has not already allocated
968 * the space with a `brk'.
970 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
971 SIGISMEMBER(psp->ps_sigonstack, sig)) {
972 sbs = (u_int64_t)td->td_sigstk.ss_sp;
973 sbs = (sbs + 15) & ~15;
974 sfp = (struct sigframe *)(sbs + td->td_sigstk.ss_size);
975 #if defined(COMPAT_43)
976 td->td_sigstk.ss_flags |= SS_ONSTACK;
979 sfp = (struct sigframe *)sp;
980 sfp = (struct sigframe *)((u_int64_t)(sfp - 1) & ~15);
982 /* Fill in the siginfo structure for POSIX handlers. */
983 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
984 sf.sf_si = ksi->ksi_info;
985 sf.sf_si.si_signo = sig;
987 * XXX this shouldn't be here after code in trap.c
990 sf.sf_si.si_addr = (void*)tf->tf_special.ifa;
991 code = (u_int64_t)&sfp->sf_si;
994 mtx_unlock(&psp->ps_mtx);
997 get_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
999 /* Copy the frame out to userland. */
1000 if (copyout(&sf, sfp, sizeof(sf)) != 0) {
1002 * Process has trashed its stack; give it an illegal
1003 * instruction to halt it in its tracks.
1006 sigexit(td, SIGILL);
1010 if ((tf->tf_flags & FRAME_SYSCALL) == 0) {
1011 tf->tf_special.psr &= ~IA64_PSR_RI;
1012 tf->tf_special.iip = ia64_get_k5() +
1013 ((uint64_t)break_sigtramp - (uint64_t)ia64_gateway_page);
1015 tf->tf_special.iip = ia64_get_k5() +
1016 ((uint64_t)epc_sigtramp - (uint64_t)ia64_gateway_page);
1019 * Setup the trapframe to return to the signal trampoline. We pass
1020 * information to the trampoline in the following registers:
1022 * gp new backing store or NULL
1024 * r9 signal code or siginfo pointer
1025 * r10 signal handler (function descriptor)
1027 tf->tf_special.sp = (u_int64_t)sfp - 16;
1028 tf->tf_special.gp = sbs;
1029 tf->tf_special.bspstore = sf.sf_uc.uc_mcontext.mc_special.bspstore;
1030 tf->tf_special.ndirty = 0;
1031 tf->tf_special.rnat = sf.sf_uc.uc_mcontext.mc_special.rnat;
1032 tf->tf_scratch.gr8 = sig;
1033 tf->tf_scratch.gr9 = code;
1034 tf->tf_scratch.gr10 = (u_int64_t)catcher;
1037 mtx_lock(&psp->ps_mtx);
1041 * System call to cleanup state after a signal
1042 * has been taken. Reset signal mask and
1043 * stack state from context left by sendsig (above).
1044 * Return to previous pc and psl as specified by
1045 * context left by sendsig. Check carefully to
1046 * make sure that the user has not modified the
1047 * state to gain improper privileges.
1052 sigreturn(struct thread *td,
1053 struct sigreturn_args /* {
1054 ucontext_t *sigcntxp;
1058 struct trapframe *tf;
1067 * Fetch the entire context structure at once for speed.
1068 * We don't use a normal argument to simplify RSE handling.
1070 if (copyin(uap->sigcntxp, (caddr_t)&uc, sizeof(uc)))
1073 set_mcontext(td, &uc.uc_mcontext);
1076 #if defined(COMPAT_43)
1077 if (sigonstack(tf->tf_special.sp))
1078 td->td_sigstk.ss_flags |= SS_ONSTACK;
1080 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
1082 td->td_sigmask = uc.uc_sigmask;
1083 SIG_CANTMASK(td->td_sigmask);
1087 return (EJUSTRETURN);
1090 #ifdef COMPAT_FREEBSD4
1092 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
1095 return sigreturn(td, (struct sigreturn_args *)uap);
1100 * Construct a PCB from a trapframe. This is called from kdb_trap() where
1101 * we want to start a backtrace from the function that caused us to enter
1102 * the debugger. We have the context in the trapframe, but base the trace
1103 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
1104 * enough for a backtrace.
1107 makectx(struct trapframe *tf, struct pcb *pcb)
1110 pcb->pcb_special = tf->tf_special;
1111 pcb->pcb_special.__spare = ~0UL; /* XXX see unwind.c */
1112 save_callee_saved(&pcb->pcb_preserved);
1113 save_callee_saved_fp(&pcb->pcb_preserved_fp);
1117 ia64_flush_dirty(struct thread *td, struct _special *r)
1121 uint64_t bspst, kstk, rnat;
1127 kstk = td->td_kstack + (r->bspstore & 0x1ffUL);
1128 if (td == curthread) {
1129 __asm __volatile("mov ar.rsc=0;;");
1130 __asm __volatile("mov %0=ar.bspstore" : "=r"(bspst));
1131 /* Make sure we have all the user registers written out. */
1132 if (bspst - kstk < r->ndirty) {
1133 __asm __volatile("flushrs;;");
1134 __asm __volatile("mov %0=ar.bspstore" : "=r"(bspst));
1136 __asm __volatile("mov %0=ar.rnat;;" : "=r"(rnat));
1137 __asm __volatile("mov ar.rsc=3");
1138 error = copyout((void*)kstk, (void*)r->bspstore, r->ndirty);
1140 r->rnat = (bspst > kstk && (bspst & 0x1ffL) < (kstk & 0x1ffL))
1141 ? *(uint64_t*)(kstk | 0x1f8L) : rnat;
1143 locked = PROC_LOCKED(td->td_proc);
1146 iov.iov_base = (void*)(uintptr_t)kstk;
1147 iov.iov_len = r->ndirty;
1150 uio.uio_offset = r->bspstore;
1151 uio.uio_resid = r->ndirty;
1152 uio.uio_segflg = UIO_SYSSPACE;
1153 uio.uio_rw = UIO_WRITE;
1155 error = proc_rwmem(td->td_proc, &uio);
1157 * XXX proc_rwmem() doesn't currently return ENOSPC,
1158 * so I think it can bogusly return 0. Neither do
1159 * we allow short writes.
1161 if (uio.uio_resid != 0 && error == 0)
1167 r->bspstore += r->ndirty;
1173 get_mcontext(struct thread *td, mcontext_t *mc, int flags)
1175 struct trapframe *tf;
1179 bzero(mc, sizeof(*mc));
1180 mc->mc_special = tf->tf_special;
1181 error = ia64_flush_dirty(td, &mc->mc_special);
1182 if (tf->tf_flags & FRAME_SYSCALL) {
1183 mc->mc_flags |= _MC_FLAGS_SYSCALL_CONTEXT;
1184 mc->mc_scratch = tf->tf_scratch;
1185 if (flags & GET_MC_CLEAR_RET) {
1186 mc->mc_scratch.gr8 = 0;
1187 mc->mc_scratch.gr9 = 0;
1188 mc->mc_scratch.gr10 = 0;
1189 mc->mc_scratch.gr11 = 0;
1192 mc->mc_flags |= _MC_FLAGS_ASYNC_CONTEXT;
1193 mc->mc_scratch = tf->tf_scratch;
1194 mc->mc_scratch_fp = tf->tf_scratch_fp;
1196 * XXX If the thread never used the high FP registers, we
1197 * probably shouldn't waste time saving them.
1199 ia64_highfp_save(td);
1200 mc->mc_flags |= _MC_FLAGS_HIGHFP_VALID;
1201 mc->mc_high_fp = td->td_pcb->pcb_high_fp;
1203 save_callee_saved(&mc->mc_preserved);
1204 save_callee_saved_fp(&mc->mc_preserved_fp);
1209 set_mcontext(struct thread *td, const mcontext_t *mc)
1212 struct trapframe *tf;
1217 KASSERT((tf->tf_special.ndirty & ~PAGE_MASK) == 0,
1218 ("Whoa there! We have more than 8KB of dirty registers!"));
1222 * Only copy the user mask and the restart instruction bit from
1225 psrmask = IA64_PSR_BE | IA64_PSR_UP | IA64_PSR_AC | IA64_PSR_MFL |
1226 IA64_PSR_MFH | IA64_PSR_RI;
1227 s.psr = (tf->tf_special.psr & ~psrmask) | (s.psr & psrmask);
1228 /* We don't have any dirty registers of the new context. */
1230 if (mc->mc_flags & _MC_FLAGS_ASYNC_CONTEXT) {
1232 * We can get an async context passed to us while we
1233 * entered the kernel through a syscall: sigreturn(2)
1234 * takes contexts that could previously be the result of
1235 * a trap or interrupt.
1236 * Hence, we cannot assert that the trapframe is not
1237 * a syscall frame, but we can assert that it's at
1238 * least an expected syscall.
1240 if (tf->tf_flags & FRAME_SYSCALL) {
1241 KASSERT(tf->tf_scratch.gr15 == SYS_sigreturn, ("foo"));
1242 tf->tf_flags &= ~FRAME_SYSCALL;
1244 tf->tf_scratch = mc->mc_scratch;
1245 tf->tf_scratch_fp = mc->mc_scratch_fp;
1246 if (mc->mc_flags & _MC_FLAGS_HIGHFP_VALID)
1247 td->td_pcb->pcb_high_fp = mc->mc_high_fp;
1249 KASSERT((tf->tf_flags & FRAME_SYSCALL) != 0, ("foo"));
1250 if ((mc->mc_flags & _MC_FLAGS_SYSCALL_CONTEXT) == 0) {
1251 s.cfm = tf->tf_special.cfm;
1252 s.iip = tf->tf_special.iip;
1253 tf->tf_scratch.gr15 = 0; /* Clear syscall nr. */
1255 tf->tf_scratch = mc->mc_scratch;
1258 restore_callee_saved(&mc->mc_preserved);
1259 restore_callee_saved_fp(&mc->mc_preserved_fp);
1265 * Clear registers on exec.
1268 exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
1270 struct trapframe *tf;
1271 uint64_t *ksttop, *kst;
1274 ksttop = (uint64_t*)(td->td_kstack + tf->tf_special.ndirty +
1275 (tf->tf_special.bspstore & 0x1ffUL));
1278 * We can ignore up to 8KB of dirty registers by masking off the
1279 * lower 13 bits in exception_restore() or epc_syscall(). This
1280 * should be enough for a couple of years, but if there are more
1281 * than 8KB of dirty registers, we lose track of the bottom of
1282 * the kernel stack. The solution is to copy the active part of
1283 * the kernel stack down 1 page (or 2, but not more than that)
1284 * so that we always have less than 8KB of dirty registers.
1286 KASSERT((tf->tf_special.ndirty & ~PAGE_MASK) == 0,
1287 ("Whoa there! We have more than 8KB of dirty registers!"));
1289 bzero(&tf->tf_special, sizeof(tf->tf_special));
1290 if ((tf->tf_flags & FRAME_SYSCALL) == 0) { /* break syscalls. */
1291 bzero(&tf->tf_scratch, sizeof(tf->tf_scratch));
1292 bzero(&tf->tf_scratch_fp, sizeof(tf->tf_scratch_fp));
1293 tf->tf_special.cfm = (1UL<<63) | (3UL<<7) | 3UL;
1294 tf->tf_special.bspstore = IA64_BACKINGSTORE;
1296 * Copy the arguments onto the kernel register stack so that
1297 * they get loaded by the loadrs instruction. Skip over the
1298 * NaT collection points.
1301 if (((uintptr_t)kst & 0x1ff) == 0x1f8)
1304 if (((uintptr_t)kst & 0x1ff) == 0x1f8)
1306 *kst-- = ps_strings;
1307 if (((uintptr_t)kst & 0x1ff) == 0x1f8)
1310 tf->tf_special.ndirty = (ksttop - kst) << 3;
1311 } else { /* epc syscalls (default). */
1312 tf->tf_special.cfm = (3UL<<62) | (3UL<<7) | 3UL;
1313 tf->tf_special.bspstore = IA64_BACKINGSTORE + 24;
1315 * Write values for out0, out1 and out2 to the user's backing
1316 * store and arrange for them to be restored into the user's
1317 * initial register frame.
1318 * Assumes that (bspstore & 0x1f8) < 0x1e0.
1320 suword((caddr_t)tf->tf_special.bspstore - 24, stack);
1321 suword((caddr_t)tf->tf_special.bspstore - 16, ps_strings);
1322 suword((caddr_t)tf->tf_special.bspstore - 8, 0);
1325 tf->tf_special.iip = entry;
1326 tf->tf_special.sp = (stack & ~15) - 16;
1327 tf->tf_special.rsc = 0xf;
1328 tf->tf_special.fpsr = IA64_FPSR_DEFAULT;
1329 tf->tf_special.psr = IA64_PSR_IC | IA64_PSR_I | IA64_PSR_IT |
1330 IA64_PSR_DT | IA64_PSR_RT | IA64_PSR_DFH | IA64_PSR_BN |
1335 ptrace_set_pc(struct thread *td, unsigned long addr)
1339 switch (addr & 0xFUL) {
1341 slot = IA64_PSR_RI_0;
1344 /* XXX we need to deal with MLX bundles here */
1345 slot = IA64_PSR_RI_1;
1348 slot = IA64_PSR_RI_2;
1354 td->td_frame->tf_special.iip = addr & ~0x0FULL;
1355 td->td_frame->tf_special.psr =
1356 (td->td_frame->tf_special.psr & ~IA64_PSR_RI) | slot;
1361 ptrace_single_step(struct thread *td)
1363 struct trapframe *tf;
1366 * There's no way to set single stepping when we're leaving the
1367 * kernel through the EPC syscall path. The way we solve this is
1368 * by enabling the lower-privilege trap so that we re-enter the
1369 * kernel as soon as the privilege level changes. See trap.c for
1370 * how we proceed from there.
1373 if (tf->tf_flags & FRAME_SYSCALL)
1374 tf->tf_special.psr |= IA64_PSR_LP;
1376 tf->tf_special.psr |= IA64_PSR_SS;
1381 ptrace_clear_single_step(struct thread *td)
1383 struct trapframe *tf;
1386 * Clear any and all status bits we may use to implement single
1390 tf->tf_special.psr &= ~IA64_PSR_SS;
1391 tf->tf_special.psr &= ~IA64_PSR_LP;
1392 tf->tf_special.psr &= ~IA64_PSR_TB;
1397 fill_regs(struct thread *td, struct reg *regs)
1399 struct trapframe *tf;
1402 regs->r_special = tf->tf_special;
1403 regs->r_scratch = tf->tf_scratch;
1404 save_callee_saved(®s->r_preserved);
1409 set_regs(struct thread *td, struct reg *regs)
1411 struct trapframe *tf;
1415 error = ia64_flush_dirty(td, &tf->tf_special);
1417 tf->tf_special = regs->r_special;
1418 tf->tf_special.bspstore += tf->tf_special.ndirty;
1419 tf->tf_special.ndirty = 0;
1420 tf->tf_scratch = regs->r_scratch;
1421 restore_callee_saved(®s->r_preserved);
1427 fill_dbregs(struct thread *td, struct dbreg *dbregs)
1434 set_dbregs(struct thread *td, struct dbreg *dbregs)
1441 fill_fpregs(struct thread *td, struct fpreg *fpregs)
1443 struct trapframe *frame = td->td_frame;
1444 struct pcb *pcb = td->td_pcb;
1446 /* Save the high FP registers. */
1447 ia64_highfp_save(td);
1449 fpregs->fpr_scratch = frame->tf_scratch_fp;
1450 save_callee_saved_fp(&fpregs->fpr_preserved);
1451 fpregs->fpr_high = pcb->pcb_high_fp;
1456 set_fpregs(struct thread *td, struct fpreg *fpregs)
1458 struct trapframe *frame = td->td_frame;
1459 struct pcb *pcb = td->td_pcb;
1461 /* Throw away the high FP registers (should be redundant). */
1462 ia64_highfp_drop(td);
1464 frame->tf_scratch_fp = fpregs->fpr_scratch;
1465 restore_callee_saved_fp(&fpregs->fpr_preserved);
1466 pcb->pcb_high_fp = fpregs->fpr_high;
1471 * High FP register functions.
1475 ia64_highfp_drop(struct thread *td)
1481 mtx_lock_spin(&td->td_md.md_highfp_mtx);
1483 cpu = pcb->pcb_fpcpu;
1485 mtx_unlock_spin(&td->td_md.md_highfp_mtx);
1488 pcb->pcb_fpcpu = NULL;
1489 thr = cpu->pc_fpcurthread;
1490 cpu->pc_fpcurthread = NULL;
1491 mtx_unlock_spin(&td->td_md.md_highfp_mtx);
1493 /* Post-mortem sanity checking. */
1494 KASSERT(thr == td, ("Inconsistent high FP state"));
1499 ia64_highfp_save(struct thread *td)
1505 /* Don't save if the high FP registers weren't modified. */
1506 if ((td->td_frame->tf_special.psr & IA64_PSR_MFH) == 0)
1507 return (ia64_highfp_drop(td));
1509 mtx_lock_spin(&td->td_md.md_highfp_mtx);
1511 cpu = pcb->pcb_fpcpu;
1513 mtx_unlock_spin(&td->td_md.md_highfp_mtx);
1517 if (td == curthread)
1520 mtx_unlock_spin(&td->td_md.md_highfp_mtx);
1521 ipi_send(cpu, IPI_HIGH_FP);
1522 if (td == curthread)
1524 while (pcb->pcb_fpcpu == cpu)
1528 save_high_fp(&pcb->pcb_high_fp);
1529 if (td == curthread)
1533 save_high_fp(&pcb->pcb_high_fp);
1535 pcb->pcb_fpcpu = NULL;
1536 thr = cpu->pc_fpcurthread;
1537 cpu->pc_fpcurthread = NULL;
1538 mtx_unlock_spin(&td->td_md.md_highfp_mtx);
1540 /* Post-mortem sanity cxhecking. */
1541 KASSERT(thr == td, ("Inconsistent high FP state"));
1546 ia64_sync_icache(vm_offset_t va, vm_offset_t sz)
1550 if (!ia64_sync_icache_needed)