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_sched.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/syscallsubr.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/cpu.h>
84 #include <machine/efi.h>
85 #include <machine/elf.h>
86 #include <machine/fpu.h>
87 #include <machine/intr.h>
88 #include <machine/mca.h>
89 #include <machine/md_var.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 SYSCTL_NODE(_hw, OID_AUTO, freq, CTLFLAG_RD, 0, "");
102 SYSCTL_NODE(_machdep, OID_AUTO, cpu, CTLFLAG_RD, 0, "");
104 static u_int bus_freq;
105 SYSCTL_UINT(_hw_freq, OID_AUTO, bus, CTLFLAG_RD, &bus_freq, 0,
106 "Bus clock frequency");
108 static u_int cpu_freq;
109 SYSCTL_UINT(_hw_freq, OID_AUTO, cpu, CTLFLAG_RD, &cpu_freq, 0,
110 "CPU clock frequency");
112 static u_int itc_freq;
113 SYSCTL_UINT(_hw_freq, OID_AUTO, itc, CTLFLAG_RD, &itc_freq, 0,
118 struct bootinfo *bootinfo;
122 extern u_int64_t kernel_text[], _end[];
124 extern u_int64_t ia64_gateway_page[];
125 extern u_int64_t break_sigtramp[];
126 extern u_int64_t epc_sigtramp[];
128 struct fpswa_iface *fpswa_iface;
130 vm_size_t ia64_pal_size;
131 vm_paddr_t ia64_pal_base;
132 vm_offset_t ia64_port_base;
134 u_int64_t ia64_lapic_addr = PAL_PIB_DEFAULT_ADDR;
136 struct ia64_pib *ia64_pib;
138 static int ia64_sync_icache_needed;
140 char machine[] = MACHINE;
141 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
143 static char cpu_model[64];
144 SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD, cpu_model, 0,
145 "The CPU model name");
147 static char cpu_family[64];
148 SYSCTL_STRING(_hw, OID_AUTO, family, CTLFLAG_RD, cpu_family, 0,
149 "The CPU family name");
152 extern vm_offset_t ksym_start, ksym_end;
156 struct msgbuf *msgbufp = NULL;
158 /* Other subsystems (e.g., ACPI) can hook this later. */
159 void (*cpu_idle_hook)(void) = NULL;
164 #define PHYSMAP_SIZE (2 * VM_PHYSSEG_MAX)
166 vm_paddr_t phys_avail[PHYSMAP_SIZE + 2];
168 /* must be 2 less so 0 0 can signal end of chunks */
169 #define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2)
171 struct kva_md_info kmi;
174 #define Ghz (1000L*Mhz)
180 char *family_name, *model_name;
181 u_int64_t features, tmp;
182 int number, revision, model, family, archrev;
185 * Assumes little-endian.
187 *(u_int64_t *) &vendor[0] = ia64_get_cpuid(0);
188 *(u_int64_t *) &vendor[8] = ia64_get_cpuid(1);
191 tmp = ia64_get_cpuid(3);
192 number = (tmp >> 0) & 0xff;
193 revision = (tmp >> 8) & 0xff;
194 model = (tmp >> 16) & 0xff;
195 family = (tmp >> 24) & 0xff;
196 archrev = (tmp >> 32) & 0xff;
198 family_name = model_name = "unknown";
201 family_name = "Itanium";
202 model_name = "Merced";
205 family_name = "Itanium 2";
208 model_name = "McKinley";
212 * Deerfield is a low-voltage variant based on the
213 * Madison core. We need circumstantial evidence
214 * (i.e. the clock frequency) to identify those.
215 * Allow for roughly 1% error margin.
217 if (cpu_freq > 990 && cpu_freq < 1010)
218 model_name = "Deerfield";
220 model_name = "Madison";
223 model_name = "Madison II";
228 ia64_sync_icache_needed = 1;
230 family_name = "Itanium 2";
233 model_name = "Montecito";
236 model_name = "Montvale";
241 snprintf(cpu_family, sizeof(cpu_family), "%s", family_name);
242 snprintf(cpu_model, sizeof(cpu_model), "%s", model_name);
244 features = ia64_get_cpuid(4);
246 printf("CPU: %s (", model_name);
248 printf("%u Mhz ", cpu_freq);
249 printf("%s)\n", family_name);
250 printf(" Origin = \"%s\" Revision = %d\n", vendor, revision);
251 printf(" Features = 0x%b\n", (u_int32_t) features,
253 "\001LB" /* long branch (brl) instruction. */
254 "\002SD" /* Spontaneous deferral. */
255 "\003AO" /* 16-byte atomic operations (ld, st, cmpxchg). */ );
259 cpu_startup(void *dummy)
263 struct pcpu_stats *pcs;
266 * Good {morning,afternoon,evening,night}.
273 printf("real memory = %ld (%ld MB)\n", ia64_ptob(Maxmem),
274 ia64_ptob(Maxmem) / 1048576);
278 * Display any holes after the first chunk of extended memory.
283 printf("Physical memory chunk(s):\n");
284 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
285 long size1 = phys_avail[indx + 1] - phys_avail[indx];
287 printf("0x%08lx - 0x%08lx, %ld bytes (%ld pages)\n",
288 phys_avail[indx], phys_avail[indx + 1] - 1, size1,
289 size1 >> PAGE_SHIFT);
293 vm_ksubmap_init(&kmi);
295 printf("avail memory = %ld (%ld MB)\n", ptoa(cnt.v_free_count),
296 ptoa(cnt.v_free_count) / 1048576);
298 if (fpswa_iface == NULL)
299 printf("Warning: no FPSWA package supplied\n");
301 printf("FPSWA Revision = 0x%lx, Entry = %p\n",
302 (long)fpswa_iface->if_rev, (void *)fpswa_iface->if_fpswa);
305 * Set up buffers, so they can be used to read disk labels.
308 vm_pager_bufferinit();
311 * Traverse the MADT to discover IOSAPIC and Local SAPIC
315 ia64_pib = pmap_mapdev(ia64_lapic_addr, sizeof(*ia64_pib));
320 * Create sysctl tree for per-CPU information.
322 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) {
323 snprintf(nodename, sizeof(nodename), "%u", pc->pc_cpuid);
324 sysctl_ctx_init(&pc->pc_md.sysctl_ctx);
325 pc->pc_md.sysctl_tree = SYSCTL_ADD_NODE(&pc->pc_md.sysctl_ctx,
326 SYSCTL_STATIC_CHILDREN(_machdep_cpu), OID_AUTO, nodename,
327 CTLFLAG_RD, NULL, "");
328 if (pc->pc_md.sysctl_tree == NULL)
331 pcs = &pc->pc_md.stats;
333 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
334 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
335 "nasts", CTLFLAG_RD, &pcs->pcs_nasts,
336 "Number of IPI_AST interrupts");
338 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
339 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
340 "nclks", CTLFLAG_RD, &pcs->pcs_nclks,
341 "Number of clock interrupts");
343 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
344 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
345 "nextints", CTLFLAG_RD, &pcs->pcs_nextints,
346 "Number of ExtINT interrupts");
348 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
349 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
350 "nhardclocks", CTLFLAG_RD, &pcs->pcs_nhardclocks,
351 "Number of IPI_HARDCLOCK interrupts");
353 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
354 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
355 "nhighfps", CTLFLAG_RD, &pcs->pcs_nhighfps,
356 "Number of IPI_HIGH_FP interrupts");
358 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
359 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
360 "nhwints", CTLFLAG_RD, &pcs->pcs_nhwints,
361 "Number of hardware (device) interrupts");
363 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
364 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
365 "npreempts", CTLFLAG_RD, &pcs->pcs_npreempts,
366 "Number of IPI_PREEMPT interrupts");
368 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
369 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
370 "nrdvs", CTLFLAG_RD, &pcs->pcs_nrdvs,
371 "Number of IPI_RENDEZVOUS interrupts");
373 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
374 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
375 "nstops", CTLFLAG_RD, &pcs->pcs_nstops,
376 "Number of IPI_STOP interrupts");
378 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
379 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
380 "nstrays", CTLFLAG_RD, &pcs->pcs_nstrays,
381 "Number of stray interrupts");
384 SYSINIT(cpu_startup, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
387 cpu_flush_dcache(void *ptr, size_t len)
391 va = (uintptr_t)ptr & ~31;
392 lim = (uintptr_t)ptr + len;
401 /* Get current clock frequency for the given cpu id. */
403 cpu_est_clockrate(int cpu_id, uint64_t *rate)
406 if (pcpu_find(cpu_id) == NULL || rate == NULL)
408 *rate = (u_long)cpu_freq * 1000000ul;
430 KASSERT(ie != 0, ("%s called with interrupts disabled\n", __func__));
432 if (sched_runnable())
434 else if (cpu_idle_hook != NULL) {
436 /* The hook must enable interrupts! */
438 ia64_call_pal_static(PAL_HALT_LIGHT, 0, 0, 0);
449 cpu_idle_wakeup(int cpu)
463 cpu_switch(struct thread *old, struct thread *new, struct mtx *mtx)
465 struct pcb *oldpcb, *newpcb;
467 oldpcb = old->td_pcb;
468 #ifdef COMPAT_FREEBSD32
469 ia32_savectx(oldpcb);
471 if (PCPU_GET(fpcurthread) == old)
472 old->td_frame->tf_special.psr |= IA64_PSR_DFH;
473 if (!savectx(oldpcb)) {
474 newpcb = new->td_pcb;
475 oldpcb->pcb_current_pmap =
476 pmap_switch(newpcb->pcb_current_pmap);
478 atomic_store_rel_ptr(&old->td_lock, mtx);
480 #if defined(SCHED_ULE) && defined(SMP)
481 while (atomic_load_acq_ptr(&new->td_lock) == &blocked_lock)
485 PCPU_SET(curthread, new);
487 #ifdef COMPAT_FREEBSD32
488 ia32_restorectx(newpcb);
491 if (PCPU_GET(fpcurthread) == new)
492 new->td_frame->tf_special.psr &= ~IA64_PSR_DFH;
494 /* We should not get here. */
495 panic("cpu_switch: restorectx() returned");
501 cpu_throw(struct thread *old __unused, struct thread *new)
505 newpcb = new->td_pcb;
506 (void)pmap_switch(newpcb->pcb_current_pmap);
508 #if defined(SCHED_ULE) && defined(SMP)
509 while (atomic_load_acq_ptr(&new->td_lock) == &blocked_lock)
513 PCPU_SET(curthread, new);
515 #ifdef COMPAT_FREEBSD32
516 ia32_restorectx(newpcb);
520 /* We should not get here. */
521 panic("cpu_throw: restorectx() returned");
526 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
530 * Set pc_acpi_id to "uninitialized".
531 * See sys/dev/acpica/acpi_cpu.c
533 pcpu->pc_acpi_id = 0xffffffff;
543 if (td->td_md.md_spinlock_count == 0) {
544 intr = intr_disable();
545 td->td_md.md_spinlock_count = 1;
546 td->td_md.md_saved_intr = intr;
548 td->td_md.md_spinlock_count++;
560 intr = td->td_md.md_saved_intr;
561 td->td_md.md_spinlock_count--;
562 if (td->td_md.md_spinlock_count == 0)
567 map_vhpt(uintptr_t vhpt)
572 pte = PTE_PRESENT | PTE_MA_WB | PTE_ACCESSED | PTE_DIRTY |
573 PTE_PL_KERN | PTE_AR_RW;
574 pte |= vhpt & PTE_PPN_MASK;
576 __asm __volatile("ptr.d %0,%1" :: "r"(vhpt),
577 "r"(pmap_vhpt_log2size << 2));
579 __asm __volatile("mov %0=psr" : "=r"(psr));
580 __asm __volatile("rsm psr.ic|psr.i");
583 ia64_set_itir(pmap_vhpt_log2size << 2);
585 __asm __volatile("itr.d dtr[%0]=%1" :: "r"(3), "r"(pte));
586 __asm __volatile("mov psr.l=%0" :: "r" (psr));
599 if (ia64_pal_size == 0)
602 va = IA64_PHYS_TO_RR7(ia64_pal_base);
611 pte = PTE_PRESENT | PTE_MA_WB | PTE_ACCESSED | PTE_DIRTY |
612 PTE_PL_KERN | PTE_AR_RWX;
613 pte |= ia64_pal_base & PTE_PPN_MASK;
615 __asm __volatile("ptr.d %0,%1; ptr.i %0,%1" :: "r"(va), "r"(shft<<2));
617 __asm __volatile("mov %0=psr" : "=r"(psr));
618 __asm __volatile("rsm psr.ic|psr.i");
621 ia64_set_itir(shft << 2);
623 __asm __volatile("itr.d dtr[%0]=%1" :: "r"(4), "r"(pte));
625 __asm __volatile("itr.i itr[%0]=%1" :: "r"(1), "r"(pte));
626 __asm __volatile("mov psr.l=%0" :: "r" (psr));
631 map_gateway_page(void)
636 pte = PTE_PRESENT | PTE_MA_WB | PTE_ACCESSED | PTE_DIRTY |
637 PTE_PL_KERN | PTE_AR_X_RX;
638 pte |= ia64_tpa((uint64_t)ia64_gateway_page) & PTE_PPN_MASK;
640 __asm __volatile("ptr.d %0,%1; ptr.i %0,%1" ::
641 "r"(VM_MAXUSER_ADDRESS), "r"(PAGE_SHIFT << 2));
643 __asm __volatile("mov %0=psr" : "=r"(psr));
644 __asm __volatile("rsm psr.ic|psr.i");
646 ia64_set_ifa(VM_MAXUSER_ADDRESS);
647 ia64_set_itir(PAGE_SHIFT << 2);
649 __asm __volatile("itr.d dtr[%0]=%1" :: "r"(5), "r"(pte));
651 __asm __volatile("itr.i itr[%0]=%1" :: "r"(2), "r"(pte));
652 __asm __volatile("mov psr.l=%0" :: "r" (psr));
655 /* Expose the mapping to userland in ar.k5 */
656 ia64_set_k5(VM_MAXUSER_ADDRESS);
660 freq_ratio(u_long base, u_long ratio)
664 f = (base * (ratio >> 32)) / (ratio & 0xfffffffful);
665 return ((f + 500000) / 1000000);
669 calculate_frequencies(void)
671 struct ia64_sal_result sal;
672 struct ia64_pal_result pal;
676 sal = ia64_sal_entry(SAL_FREQ_BASE, 0, 0, 0, 0, 0, 0, 0);
677 pal = ia64_call_pal_static(PAL_FREQ_RATIOS, 0, 0, 0);
680 if (sal.sal_status == 0 && pal.pal_status == 0) {
682 printf("Platform clock frequency %ld Hz\n",
684 printf("Processor ratio %ld/%ld, Bus ratio %ld/%ld, "
685 "ITC ratio %ld/%ld\n",
686 pal.pal_result[0] >> 32,
687 pal.pal_result[0] & ((1L << 32) - 1),
688 pal.pal_result[1] >> 32,
689 pal.pal_result[1] & ((1L << 32) - 1),
690 pal.pal_result[2] >> 32,
691 pal.pal_result[2] & ((1L << 32) - 1));
693 cpu_freq = freq_ratio(sal.sal_result[0], pal.pal_result[0]);
694 bus_freq = freq_ratio(sal.sal_result[0], pal.pal_result[1]);
695 itc_freq = freq_ratio(sal.sal_result[0], pal.pal_result[2]);
699 struct ia64_init_return
702 struct ia64_init_return ret;
704 vm_offset_t kernstart, kernend;
705 vm_offset_t kernstartpfn, kernendpfn, pfn0, pfn1;
708 int metadata_missing;
710 /* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
713 * TODO: Disable interrupts, floating point etc.
714 * Maybe flush cache and tlb
716 ia64_set_fpsr(IA64_FPSR_DEFAULT);
719 * TODO: Get critical system information (if possible, from the
720 * information provided by the boot program).
724 * Look for the I/O ports first - we need them for console
727 for (md = efi_md_first(); md != NULL; md = efi_md_next(md)) {
728 switch (md->md_type) {
729 case EFI_MD_TYPE_IOPORT:
730 ia64_port_base = (uintptr_t)pmap_mapdev(md->md_phys,
731 md->md_pages * EFI_PAGE_SIZE);
733 case EFI_MD_TYPE_PALCODE:
734 ia64_pal_size = md->md_pages * EFI_PAGE_SIZE;
735 ia64_pal_base = md->md_phys;
740 metadata_missing = 0;
741 if (bootinfo->bi_modulep)
742 preload_metadata = (caddr_t)bootinfo->bi_modulep;
744 metadata_missing = 1;
746 if (envmode == 0 && bootinfo->bi_envp)
747 kern_envp = (caddr_t)bootinfo->bi_envp;
749 kern_envp = static_env;
752 * Look at arguments passed to us and compute boothowto.
754 boothowto = bootinfo->bi_boothowto;
756 if (boothowto & RB_VERBOSE)
760 * Find the beginning and end of the kernel.
762 kernstart = trunc_page(kernel_text);
764 ksym_start = bootinfo->bi_symtab;
765 ksym_end = bootinfo->bi_esymtab;
766 kernend = (vm_offset_t)round_page(ksym_end);
768 kernend = (vm_offset_t)round_page(_end);
770 /* But if the bootstrap tells us otherwise, believe it! */
771 if (bootinfo->bi_kernend)
772 kernend = round_page(bootinfo->bi_kernend);
775 * Region 6 is direct mapped UC and region 7 is direct mapped
776 * WC. The details of this is controlled by the Alt {I,D}TLB
777 * handlers. Here we just make sure that they have the largest
778 * possible page size to minimise TLB usage.
780 ia64_set_rr(IA64_RR_BASE(6), (6 << 8) | (PAGE_SHIFT << 2));
781 ia64_set_rr(IA64_RR_BASE(7), (7 << 8) | (PAGE_SHIFT << 2));
785 * Wire things up so we can call the firmware.
788 efi_boot_minimal(bootinfo->bi_systab);
791 calculate_frequencies();
793 set_cputicker(ia64_get_itc, (u_long)itc_freq * 1000000, 0);
796 * Setup the PCPU data for the bootstrap processor. It is needed
797 * by printf(). Also, since printf() has critical sections, we
798 * need to initialize at least pc_curthread.
801 ia64_set_k4((u_int64_t)pcpup);
802 pcpu_init(pcpup, 0, sizeof(pcpu0));
803 dpcpu_init((void *)kernend, 0);
804 PCPU_SET(md.lid, ia64_get_lid());
805 kernend += DPCPU_SIZE;
806 PCPU_SET(curthread, &thread0);
809 * Initialize the console before we print anything out.
813 /* OUTPUT NOW ALLOWED */
815 if (metadata_missing)
816 printf("WARNING: loader(8) metadata is missing!\n");
818 /* Get FPSWA interface */
819 fpswa_iface = (bootinfo->bi_fpswa == 0) ? NULL :
820 (struct fpswa_iface *)IA64_PHYS_TO_RR7(bootinfo->bi_fpswa);
822 /* Init basic tunables, including hz */
825 p = getenv("kernelname");
827 strlcpy(kernelname, p, sizeof(kernelname));
831 kernstartpfn = atop(IA64_RR_MASK(kernstart));
832 kernendpfn = atop(IA64_RR_MASK(kernend));
835 * Size the memory regions and load phys_avail[] with the results.
839 * Find out how much memory is available, by looking at
840 * the memory descriptors.
844 printf("Memory descriptor count: %d\n", mdcount);
848 for (md = efi_md_first(); md != NULL; md = efi_md_next(md)) {
850 printf("MD %p: type %d pa 0x%lx cnt 0x%lx\n", md,
851 md->md_type, md->md_phys, md->md_pages);
854 pfn0 = ia64_btop(round_page(md->md_phys));
855 pfn1 = ia64_btop(trunc_page(md->md_phys + md->md_pages * 4096));
859 if (md->md_type != EFI_MD_TYPE_FREE)
863 * We have a memory descriptor that describes conventional
864 * memory that is for general use. We must determine if the
865 * loader has put the kernel in this region.
867 physmem += (pfn1 - pfn0);
868 if (pfn0 <= kernendpfn && kernstartpfn <= pfn1) {
870 * Must compute the location of the kernel
871 * within the segment.
874 printf("Descriptor %p contains kernel\n", mp);
876 if (pfn0 < kernstartpfn) {
878 * There is a chunk before the kernel.
881 printf("Loading chunk before kernel: "
882 "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
884 phys_avail[phys_avail_cnt] = ia64_ptob(pfn0);
885 phys_avail[phys_avail_cnt+1] = ia64_ptob(kernstartpfn);
888 if (kernendpfn < pfn1) {
890 * There is a chunk after the kernel.
893 printf("Loading chunk after kernel: "
894 "0x%lx / 0x%lx\n", kernendpfn, pfn1);
896 phys_avail[phys_avail_cnt] = ia64_ptob(kernendpfn);
897 phys_avail[phys_avail_cnt+1] = ia64_ptob(pfn1);
902 * Just load this cluster as one chunk.
905 printf("Loading descriptor %d: 0x%lx / 0x%lx\n", i,
908 phys_avail[phys_avail_cnt] = ia64_ptob(pfn0);
909 phys_avail[phys_avail_cnt+1] = ia64_ptob(pfn1);
914 phys_avail[phys_avail_cnt] = 0;
917 init_param2(physmem);
920 * Initialize error message buffer (at end of core).
922 msgbufp = (struct msgbuf *)pmap_steal_memory(msgbufsize);
923 msgbufinit(msgbufp, msgbufsize);
925 proc_linkup0(&proc0, &thread0);
927 * Init mapping for kernel stack for proc 0
929 thread0.td_kstack = pmap_steal_memory(KSTACK_PAGES * PAGE_SIZE);
930 thread0.td_kstack_pages = KSTACK_PAGES;
935 * Initialize the rest of proc 0's PCB.
937 * Set the kernel sp, reserving space for an (empty) trapframe,
938 * and make proc0's trapframe pointer point to it for sanity.
939 * Initialise proc0's backing store to start after u area.
941 cpu_thread_alloc(&thread0);
942 thread0.td_frame->tf_flags = FRAME_SYSCALL;
943 thread0.td_pcb->pcb_special.sp =
944 (u_int64_t)thread0.td_frame - 16;
945 thread0.td_pcb->pcb_special.bspstore = thread0.td_kstack;
948 * Initialize the virtual memory system.
953 * Initialize debuggers, and break into them if appropriate.
958 if (boothowto & RB_KDB)
959 kdb_enter(KDB_WHY_BOOTFLAGS,
960 "Boot flags requested debugger\n");
966 ret.bspstore = thread0.td_pcb->pcb_special.bspstore;
967 ret.sp = thread0.td_pcb->pcb_special.sp;
975 return (bootinfo->bi_hcdp);
979 bzero(void *buf, size_t len)
983 while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
987 while (len >= sizeof(u_long) * 8) {
989 *((u_long*) p + 1) = 0;
990 *((u_long*) p + 2) = 0;
991 *((u_long*) p + 3) = 0;
992 len -= sizeof(u_long) * 8;
993 *((u_long*) p + 4) = 0;
994 *((u_long*) p + 5) = 0;
995 *((u_long*) p + 6) = 0;
996 *((u_long*) p + 7) = 0;
997 p += sizeof(u_long) * 8;
999 while (len >= sizeof(u_long)) {
1001 len -= sizeof(u_long);
1002 p += sizeof(u_long);
1020 u_int64_t start, end, now;
1024 start = ia64_get_itc();
1025 end = start + itc_freq * n;
1026 /* printf("DELAY from 0x%lx to 0x%lx\n", start, end); */
1028 now = ia64_get_itc();
1029 } while (now < end || (now > start && end < start));
1035 * Send an interrupt (signal) to a process.
1038 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
1042 struct trapframe *tf;
1043 struct sigacts *psp;
1044 struct sigframe sf, *sfp;
1052 PROC_LOCK_ASSERT(p, MA_OWNED);
1053 sig = ksi->ksi_signo;
1054 code = ksi->ksi_code;
1056 mtx_assert(&psp->ps_mtx, MA_OWNED);
1058 sp = tf->tf_special.sp;
1059 oonstack = sigonstack(sp);
1062 /* save user context */
1063 bzero(&sf, sizeof(struct sigframe));
1064 sf.sf_uc.uc_sigmask = *mask;
1065 sf.sf_uc.uc_stack = td->td_sigstk;
1066 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
1067 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1070 * Allocate and validate space for the signal handler
1071 * context. Note that if the stack is in P0 space, the
1072 * call to grow() is a nop, and the useracc() check
1073 * will fail if the process has not already allocated
1074 * the space with a `brk'.
1076 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
1077 SIGISMEMBER(psp->ps_sigonstack, sig)) {
1078 sbs = (u_int64_t)td->td_sigstk.ss_sp;
1079 sbs = (sbs + 15) & ~15;
1080 sfp = (struct sigframe *)(sbs + td->td_sigstk.ss_size);
1081 #if defined(COMPAT_43)
1082 td->td_sigstk.ss_flags |= SS_ONSTACK;
1085 sfp = (struct sigframe *)sp;
1086 sfp = (struct sigframe *)((u_int64_t)(sfp - 1) & ~15);
1088 /* Fill in the siginfo structure for POSIX handlers. */
1089 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
1090 sf.sf_si = ksi->ksi_info;
1091 sf.sf_si.si_signo = sig;
1093 * XXX this shouldn't be here after code in trap.c
1096 sf.sf_si.si_addr = (void*)tf->tf_special.ifa;
1097 code = (u_int64_t)&sfp->sf_si;
1100 mtx_unlock(&psp->ps_mtx);
1103 get_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
1105 /* Copy the frame out to userland. */
1106 if (copyout(&sf, sfp, sizeof(sf)) != 0) {
1108 * Process has trashed its stack; give it an illegal
1109 * instruction to halt it in its tracks.
1112 sigexit(td, SIGILL);
1116 if ((tf->tf_flags & FRAME_SYSCALL) == 0) {
1117 tf->tf_special.psr &= ~IA64_PSR_RI;
1118 tf->tf_special.iip = ia64_get_k5() +
1119 ((uint64_t)break_sigtramp - (uint64_t)ia64_gateway_page);
1121 tf->tf_special.iip = ia64_get_k5() +
1122 ((uint64_t)epc_sigtramp - (uint64_t)ia64_gateway_page);
1125 * Setup the trapframe to return to the signal trampoline. We pass
1126 * information to the trampoline in the following registers:
1128 * gp new backing store or NULL
1130 * r9 signal code or siginfo pointer
1131 * r10 signal handler (function descriptor)
1133 tf->tf_special.sp = (u_int64_t)sfp - 16;
1134 tf->tf_special.gp = sbs;
1135 tf->tf_special.bspstore = sf.sf_uc.uc_mcontext.mc_special.bspstore;
1136 tf->tf_special.ndirty = 0;
1137 tf->tf_special.rnat = sf.sf_uc.uc_mcontext.mc_special.rnat;
1138 tf->tf_scratch.gr8 = sig;
1139 tf->tf_scratch.gr9 = code;
1140 tf->tf_scratch.gr10 = (u_int64_t)catcher;
1143 mtx_lock(&psp->ps_mtx);
1147 * System call to cleanup state after a signal
1148 * has been taken. Reset signal mask and
1149 * stack state from context left by sendsig (above).
1150 * Return to previous pc and psl as specified by
1151 * context left by sendsig. Check carefully to
1152 * make sure that the user has not modified the
1153 * state to gain improper privileges.
1158 sys_sigreturn(struct thread *td,
1159 struct sigreturn_args /* {
1160 ucontext_t *sigcntxp;
1164 struct trapframe *tf;
1171 * Fetch the entire context structure at once for speed.
1172 * We don't use a normal argument to simplify RSE handling.
1174 if (copyin(uap->sigcntxp, (caddr_t)&uc, sizeof(uc)))
1177 set_mcontext(td, &uc.uc_mcontext);
1179 #if defined(COMPAT_43)
1180 if (sigonstack(tf->tf_special.sp))
1181 td->td_sigstk.ss_flags |= SS_ONSTACK;
1183 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
1185 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
1187 return (EJUSTRETURN);
1190 #ifdef COMPAT_FREEBSD4
1192 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
1195 return sys_sigreturn(td, (struct sigreturn_args *)uap);
1200 * Construct a PCB from a trapframe. This is called from kdb_trap() where
1201 * we want to start a backtrace from the function that caused us to enter
1202 * the debugger. We have the context in the trapframe, but base the trace
1203 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
1204 * enough for a backtrace.
1207 makectx(struct trapframe *tf, struct pcb *pcb)
1210 pcb->pcb_special = tf->tf_special;
1211 pcb->pcb_special.__spare = ~0UL; /* XXX see unwind.c */
1212 save_callee_saved(&pcb->pcb_preserved);
1213 save_callee_saved_fp(&pcb->pcb_preserved_fp);
1217 ia64_flush_dirty(struct thread *td, struct _special *r)
1221 uint64_t bspst, kstk, rnat;
1227 kstk = td->td_kstack + (r->bspstore & 0x1ffUL);
1228 if (td == curthread) {
1229 __asm __volatile("mov ar.rsc=0;;");
1230 __asm __volatile("mov %0=ar.bspstore" : "=r"(bspst));
1231 /* Make sure we have all the user registers written out. */
1232 if (bspst - kstk < r->ndirty) {
1233 __asm __volatile("flushrs;;");
1234 __asm __volatile("mov %0=ar.bspstore" : "=r"(bspst));
1236 __asm __volatile("mov %0=ar.rnat;;" : "=r"(rnat));
1237 __asm __volatile("mov ar.rsc=3");
1238 error = copyout((void*)kstk, (void*)r->bspstore, r->ndirty);
1240 r->rnat = (bspst > kstk && (bspst & 0x1ffL) < (kstk & 0x1ffL))
1241 ? *(uint64_t*)(kstk | 0x1f8L) : rnat;
1243 locked = PROC_LOCKED(td->td_proc);
1246 iov.iov_base = (void*)(uintptr_t)kstk;
1247 iov.iov_len = r->ndirty;
1250 uio.uio_offset = r->bspstore;
1251 uio.uio_resid = r->ndirty;
1252 uio.uio_segflg = UIO_SYSSPACE;
1253 uio.uio_rw = UIO_WRITE;
1255 error = proc_rwmem(td->td_proc, &uio);
1257 * XXX proc_rwmem() doesn't currently return ENOSPC,
1258 * so I think it can bogusly return 0. Neither do
1259 * we allow short writes.
1261 if (uio.uio_resid != 0 && error == 0)
1267 r->bspstore += r->ndirty;
1273 get_mcontext(struct thread *td, mcontext_t *mc, int flags)
1275 struct trapframe *tf;
1279 bzero(mc, sizeof(*mc));
1280 mc->mc_special = tf->tf_special;
1281 error = ia64_flush_dirty(td, &mc->mc_special);
1282 if (tf->tf_flags & FRAME_SYSCALL) {
1283 mc->mc_flags |= _MC_FLAGS_SYSCALL_CONTEXT;
1284 mc->mc_scratch = tf->tf_scratch;
1285 if (flags & GET_MC_CLEAR_RET) {
1286 mc->mc_scratch.gr8 = 0;
1287 mc->mc_scratch.gr9 = 0;
1288 mc->mc_scratch.gr10 = 0;
1289 mc->mc_scratch.gr11 = 0;
1292 mc->mc_flags |= _MC_FLAGS_ASYNC_CONTEXT;
1293 mc->mc_scratch = tf->tf_scratch;
1294 mc->mc_scratch_fp = tf->tf_scratch_fp;
1296 * XXX If the thread never used the high FP registers, we
1297 * probably shouldn't waste time saving them.
1299 ia64_highfp_save(td);
1300 mc->mc_flags |= _MC_FLAGS_HIGHFP_VALID;
1301 mc->mc_high_fp = td->td_pcb->pcb_high_fp;
1303 save_callee_saved(&mc->mc_preserved);
1304 save_callee_saved_fp(&mc->mc_preserved_fp);
1309 set_mcontext(struct thread *td, const mcontext_t *mc)
1312 struct trapframe *tf;
1317 KASSERT((tf->tf_special.ndirty & ~PAGE_MASK) == 0,
1318 ("Whoa there! We have more than 8KB of dirty registers!"));
1322 * Only copy the user mask and the restart instruction bit from
1325 psrmask = IA64_PSR_BE | IA64_PSR_UP | IA64_PSR_AC | IA64_PSR_MFL |
1326 IA64_PSR_MFH | IA64_PSR_RI;
1327 s.psr = (tf->tf_special.psr & ~psrmask) | (s.psr & psrmask);
1328 /* We don't have any dirty registers of the new context. */
1330 if (mc->mc_flags & _MC_FLAGS_ASYNC_CONTEXT) {
1332 * We can get an async context passed to us while we
1333 * entered the kernel through a syscall: sigreturn(2)
1334 * takes contexts that could previously be the result of
1335 * a trap or interrupt.
1336 * Hence, we cannot assert that the trapframe is not
1337 * a syscall frame, but we can assert that it's at
1338 * least an expected syscall.
1340 if (tf->tf_flags & FRAME_SYSCALL) {
1341 KASSERT(tf->tf_scratch.gr15 == SYS_sigreturn, ("foo"));
1342 tf->tf_flags &= ~FRAME_SYSCALL;
1344 tf->tf_scratch = mc->mc_scratch;
1345 tf->tf_scratch_fp = mc->mc_scratch_fp;
1346 if (mc->mc_flags & _MC_FLAGS_HIGHFP_VALID)
1347 td->td_pcb->pcb_high_fp = mc->mc_high_fp;
1349 KASSERT((tf->tf_flags & FRAME_SYSCALL) != 0, ("foo"));
1350 if ((mc->mc_flags & _MC_FLAGS_SYSCALL_CONTEXT) == 0) {
1351 s.cfm = tf->tf_special.cfm;
1352 s.iip = tf->tf_special.iip;
1353 tf->tf_scratch.gr15 = 0; /* Clear syscall nr. */
1355 tf->tf_scratch = mc->mc_scratch;
1358 restore_callee_saved(&mc->mc_preserved);
1359 restore_callee_saved_fp(&mc->mc_preserved_fp);
1365 * Clear registers on exec.
1368 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
1370 struct trapframe *tf;
1371 uint64_t *ksttop, *kst;
1374 ksttop = (uint64_t*)(td->td_kstack + tf->tf_special.ndirty +
1375 (tf->tf_special.bspstore & 0x1ffUL));
1378 * We can ignore up to 8KB of dirty registers by masking off the
1379 * lower 13 bits in exception_restore() or epc_syscall(). This
1380 * should be enough for a couple of years, but if there are more
1381 * than 8KB of dirty registers, we lose track of the bottom of
1382 * the kernel stack. The solution is to copy the active part of
1383 * the kernel stack down 1 page (or 2, but not more than that)
1384 * so that we always have less than 8KB of dirty registers.
1386 KASSERT((tf->tf_special.ndirty & ~PAGE_MASK) == 0,
1387 ("Whoa there! We have more than 8KB of dirty registers!"));
1389 bzero(&tf->tf_special, sizeof(tf->tf_special));
1390 if ((tf->tf_flags & FRAME_SYSCALL) == 0) { /* break syscalls. */
1391 bzero(&tf->tf_scratch, sizeof(tf->tf_scratch));
1392 bzero(&tf->tf_scratch_fp, sizeof(tf->tf_scratch_fp));
1393 tf->tf_special.cfm = (1UL<<63) | (3UL<<7) | 3UL;
1394 tf->tf_special.bspstore = IA64_BACKINGSTORE;
1396 * Copy the arguments onto the kernel register stack so that
1397 * they get loaded by the loadrs instruction. Skip over the
1398 * NaT collection points.
1401 if (((uintptr_t)kst & 0x1ff) == 0x1f8)
1404 if (((uintptr_t)kst & 0x1ff) == 0x1f8)
1406 *kst-- = imgp->ps_strings;
1407 if (((uintptr_t)kst & 0x1ff) == 0x1f8)
1410 tf->tf_special.ndirty = (ksttop - kst) << 3;
1411 } else { /* epc syscalls (default). */
1412 tf->tf_special.cfm = (3UL<<62) | (3UL<<7) | 3UL;
1413 tf->tf_special.bspstore = IA64_BACKINGSTORE + 24;
1415 * Write values for out0, out1 and out2 to the user's backing
1416 * store and arrange for them to be restored into the user's
1417 * initial register frame.
1418 * Assumes that (bspstore & 0x1f8) < 0x1e0.
1420 suword((caddr_t)tf->tf_special.bspstore - 24, stack);
1421 suword((caddr_t)tf->tf_special.bspstore - 16, imgp->ps_strings);
1422 suword((caddr_t)tf->tf_special.bspstore - 8, 0);
1425 tf->tf_special.iip = imgp->entry_addr;
1426 tf->tf_special.sp = (stack & ~15) - 16;
1427 tf->tf_special.rsc = 0xf;
1428 tf->tf_special.fpsr = IA64_FPSR_DEFAULT;
1429 tf->tf_special.psr = IA64_PSR_IC | IA64_PSR_I | IA64_PSR_IT |
1430 IA64_PSR_DT | IA64_PSR_RT | IA64_PSR_DFH | IA64_PSR_BN |
1435 ptrace_set_pc(struct thread *td, unsigned long addr)
1439 switch (addr & 0xFUL) {
1441 slot = IA64_PSR_RI_0;
1444 /* XXX we need to deal with MLX bundles here */
1445 slot = IA64_PSR_RI_1;
1448 slot = IA64_PSR_RI_2;
1454 td->td_frame->tf_special.iip = addr & ~0x0FULL;
1455 td->td_frame->tf_special.psr =
1456 (td->td_frame->tf_special.psr & ~IA64_PSR_RI) | slot;
1461 ptrace_single_step(struct thread *td)
1463 struct trapframe *tf;
1466 * There's no way to set single stepping when we're leaving the
1467 * kernel through the EPC syscall path. The way we solve this is
1468 * by enabling the lower-privilege trap so that we re-enter the
1469 * kernel as soon as the privilege level changes. See trap.c for
1470 * how we proceed from there.
1473 if (tf->tf_flags & FRAME_SYSCALL)
1474 tf->tf_special.psr |= IA64_PSR_LP;
1476 tf->tf_special.psr |= IA64_PSR_SS;
1481 ptrace_clear_single_step(struct thread *td)
1483 struct trapframe *tf;
1486 * Clear any and all status bits we may use to implement single
1490 tf->tf_special.psr &= ~IA64_PSR_SS;
1491 tf->tf_special.psr &= ~IA64_PSR_LP;
1492 tf->tf_special.psr &= ~IA64_PSR_TB;
1497 fill_regs(struct thread *td, struct reg *regs)
1499 struct trapframe *tf;
1502 regs->r_special = tf->tf_special;
1503 regs->r_scratch = tf->tf_scratch;
1504 save_callee_saved(®s->r_preserved);
1509 set_regs(struct thread *td, struct reg *regs)
1511 struct trapframe *tf;
1515 error = ia64_flush_dirty(td, &tf->tf_special);
1517 tf->tf_special = regs->r_special;
1518 tf->tf_special.bspstore += tf->tf_special.ndirty;
1519 tf->tf_special.ndirty = 0;
1520 tf->tf_scratch = regs->r_scratch;
1521 restore_callee_saved(®s->r_preserved);
1527 fill_dbregs(struct thread *td, struct dbreg *dbregs)
1534 set_dbregs(struct thread *td, struct dbreg *dbregs)
1541 fill_fpregs(struct thread *td, struct fpreg *fpregs)
1543 struct trapframe *frame = td->td_frame;
1544 struct pcb *pcb = td->td_pcb;
1546 /* Save the high FP registers. */
1547 ia64_highfp_save(td);
1549 fpregs->fpr_scratch = frame->tf_scratch_fp;
1550 save_callee_saved_fp(&fpregs->fpr_preserved);
1551 fpregs->fpr_high = pcb->pcb_high_fp;
1556 set_fpregs(struct thread *td, struct fpreg *fpregs)
1558 struct trapframe *frame = td->td_frame;
1559 struct pcb *pcb = td->td_pcb;
1561 /* Throw away the high FP registers (should be redundant). */
1562 ia64_highfp_drop(td);
1564 frame->tf_scratch_fp = fpregs->fpr_scratch;
1565 restore_callee_saved_fp(&fpregs->fpr_preserved);
1566 pcb->pcb_high_fp = fpregs->fpr_high;
1571 ia64_sync_icache(vm_offset_t va, vm_offset_t sz)
1575 if (!ia64_sync_icache_needed)