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/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/cpu.h>
83 #include <machine/efi.h>
84 #include <machine/elf.h>
85 #include <machine/fpu.h>
86 #include <machine/intr.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 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 u_int64_t pa_bootinfo;
119 struct bootinfo bootinfo;
123 extern u_int64_t kernel_text[], _end[];
125 extern u_int64_t ia64_gateway_page[];
126 extern u_int64_t break_sigtramp[];
127 extern u_int64_t epc_sigtramp[];
129 struct fpswa_iface *fpswa_iface;
131 u_int64_t ia64_pal_base;
132 u_int64_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";
238 snprintf(cpu_family, sizeof(cpu_family), "%s", family_name);
239 snprintf(cpu_model, sizeof(cpu_model), "%s", model_name);
241 features = ia64_get_cpuid(4);
243 printf("CPU: %s (", model_name);
245 printf("%u Mhz ", cpu_freq);
246 printf("%s)\n", family_name);
247 printf(" Origin = \"%s\" Revision = %d\n", vendor, revision);
248 printf(" Features = 0x%b\n", (u_int32_t) features,
250 "\001LB" /* long branch (brl) instruction. */
251 "\002SD" /* Spontaneous deferral. */
252 "\003AO" /* 16-byte atomic operations (ld, st, cmpxchg). */ );
256 cpu_startup(void *dummy)
260 struct pcpu_stats *pcs;
263 * Good {morning,afternoon,evening,night}.
270 printf("real memory = %ld (%ld MB)\n", ia64_ptob(Maxmem),
271 ia64_ptob(Maxmem) / 1048576);
275 * Display any holes after the first chunk of extended memory.
280 printf("Physical memory chunk(s):\n");
281 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
282 long size1 = phys_avail[indx + 1] - phys_avail[indx];
284 printf("0x%08lx - 0x%08lx, %ld bytes (%ld pages)\n",
285 phys_avail[indx], phys_avail[indx + 1] - 1, size1,
286 size1 >> PAGE_SHIFT);
290 vm_ksubmap_init(&kmi);
292 printf("avail memory = %ld (%ld MB)\n", ptoa(cnt.v_free_count),
293 ptoa(cnt.v_free_count) / 1048576);
295 if (fpswa_iface == NULL)
296 printf("Warning: no FPSWA package supplied\n");
298 printf("FPSWA Revision = 0x%lx, Entry = %p\n",
299 (long)fpswa_iface->if_rev, (void *)fpswa_iface->if_fpswa);
302 * Set up buffers, so they can be used to read disk labels.
305 vm_pager_bufferinit();
308 * Traverse the MADT to discover IOSAPIC and Local SAPIC
312 ia64_pib = pmap_mapdev(ia64_lapic_addr, sizeof(*ia64_pib));
317 * Create sysctl tree for per-CPU information.
319 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
320 snprintf(nodename, sizeof(nodename), "%u", pc->pc_cpuid);
321 sysctl_ctx_init(&pc->pc_md.sysctl_ctx);
322 pc->pc_md.sysctl_tree = SYSCTL_ADD_NODE(&pc->pc_md.sysctl_ctx,
323 SYSCTL_STATIC_CHILDREN(_machdep_cpu), OID_AUTO, nodename,
324 CTLFLAG_RD, NULL, "");
325 if (pc->pc_md.sysctl_tree == NULL)
328 pcs = &pc->pc_md.stats;
330 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
331 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
332 "nasts", CTLFLAG_RD, &pcs->pcs_nasts,
333 "Number of IPI_AST interrupts");
335 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
336 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
337 "nclks", CTLFLAG_RD, &pcs->pcs_nclks,
338 "Number of clock interrupts");
340 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
341 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
342 "nextints", CTLFLAG_RD, &pcs->pcs_nextints,
343 "Number of ExtINT interrupts");
345 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
346 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
347 "nhighfps", CTLFLAG_RD, &pcs->pcs_nhighfps,
348 "Number of IPI_HIGH_FP interrupts");
350 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
351 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
352 "nhwints", CTLFLAG_RD, &pcs->pcs_nhwints,
353 "Number of hardware (device) interrupts");
355 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
356 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
357 "npreempts", CTLFLAG_RD, &pcs->pcs_npreempts,
358 "Number of IPI_PREEMPT interrupts");
360 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
361 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
362 "nrdvs", CTLFLAG_RD, &pcs->pcs_nrdvs,
363 "Number of IPI_RENDEZVOUS interrupts");
365 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
366 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
367 "nstops", CTLFLAG_RD, &pcs->pcs_nstops,
368 "Number of IPI_STOP interrupts");
370 SYSCTL_ADD_ULONG(&pc->pc_md.sysctl_ctx,
371 SYSCTL_CHILDREN(pc->pc_md.sysctl_tree), OID_AUTO,
372 "nstrays", CTLFLAG_RD, &pcs->pcs_nstrays,
373 "Number of stray interrupts");
376 SYSINIT(cpu_startup, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
379 cpu_flush_dcache(void *ptr, size_t len)
383 va = (uintptr_t)ptr & ~31;
384 lim = (uintptr_t)ptr + len;
393 /* Get current clock frequency for the given cpu id. */
395 cpu_est_clockrate(int cpu_id, uint64_t *rate)
398 if (pcpu_find(cpu_id) == NULL || rate == NULL)
400 *rate = (u_long)cpu_freq * 1000000ul;
414 struct ia64_pal_result res;
416 if (cpu_idle_hook != NULL)
419 res = ia64_call_pal_static(PAL_HALT_LIGHT, 0, 0, 0);
423 cpu_idle_wakeup(int cpu)
437 cpu_switch(struct thread *old, struct thread *new, struct mtx *mtx)
439 struct pcb *oldpcb, *newpcb;
441 oldpcb = old->td_pcb;
442 #ifdef COMPAT_FREEBSD32
443 ia32_savectx(oldpcb);
445 if (PCPU_GET(fpcurthread) == old)
446 old->td_frame->tf_special.psr |= IA64_PSR_DFH;
447 if (!savectx(oldpcb)) {
448 atomic_store_rel_ptr(&old->td_lock, mtx);
450 newpcb = new->td_pcb;
451 oldpcb->pcb_current_pmap =
452 pmap_switch(newpcb->pcb_current_pmap);
454 #if defined(SCHED_ULE) && defined(SMP)
455 while (atomic_load_acq_ptr(&new->td_lock) == &blocked_lock)
459 PCPU_SET(curthread, new);
461 #ifdef COMPAT_FREEBSD32
462 ia32_restorectx(newpcb);
465 if (PCPU_GET(fpcurthread) == new)
466 new->td_frame->tf_special.psr &= ~IA64_PSR_DFH;
468 /* We should not get here. */
469 panic("cpu_switch: restorectx() returned");
475 cpu_throw(struct thread *old __unused, struct thread *new)
479 newpcb = new->td_pcb;
480 (void)pmap_switch(newpcb->pcb_current_pmap);
482 #if defined(SCHED_ULE) && defined(SMP)
483 while (atomic_load_acq_ptr(&new->td_lock) == &blocked_lock)
487 PCPU_SET(curthread, new);
489 #ifdef COMPAT_FREEBSD32
490 ia32_restorectx(newpcb);
494 /* We should not get here. */
495 panic("cpu_throw: restorectx() returned");
500 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
504 * Set pc_acpi_id to "uninitialized".
505 * See sys/dev/acpica/acpi_cpu.c
507 pcpu->pc_acpi_id = 0xffffffff;
517 if (td->td_md.md_spinlock_count == 0) {
518 intr = intr_disable();
519 td->td_md.md_spinlock_count = 1;
520 td->td_md.md_saved_intr = intr;
522 td->td_md.md_spinlock_count++;
534 intr = td->td_md.md_saved_intr;
535 td->td_md.md_spinlock_count--;
536 if (td->td_md.md_spinlock_count == 0)
541 map_vhpt(uintptr_t vhpt)
546 pte = PTE_PRESENT | PTE_MA_WB | PTE_ACCESSED | PTE_DIRTY |
547 PTE_PL_KERN | PTE_AR_RW;
548 pte |= vhpt & PTE_PPN_MASK;
550 __asm __volatile("ptr.d %0,%1" :: "r"(vhpt),
551 "r"(IA64_ID_PAGE_SHIFT<<2));
553 __asm __volatile("mov %0=psr" : "=r"(psr));
554 __asm __volatile("rsm psr.ic|psr.i");
557 ia64_set_itir(IA64_ID_PAGE_SHIFT << 2);
559 __asm __volatile("itr.d dtr[%0]=%1" :: "r"(2), "r"(pte));
560 __asm __volatile("mov psr.l=%0" :: "r" (psr));
570 if (ia64_pal_base == 0)
573 pte = PTE_PRESENT | PTE_MA_WB | PTE_ACCESSED | PTE_DIRTY |
574 PTE_PL_KERN | PTE_AR_RWX;
575 pte |= ia64_pal_base & PTE_PPN_MASK;
577 __asm __volatile("ptr.d %0,%1; ptr.i %0,%1" ::
578 "r"(IA64_PHYS_TO_RR7(ia64_pal_base)), "r"(IA64_ID_PAGE_SHIFT<<2));
580 __asm __volatile("mov %0=psr" : "=r"(psr));
581 __asm __volatile("rsm psr.ic|psr.i");
583 ia64_set_ifa(IA64_PHYS_TO_RR7(ia64_pal_base));
584 ia64_set_itir(IA64_ID_PAGE_SHIFT << 2);
586 __asm __volatile("itr.d dtr[%0]=%1" :: "r"(1), "r"(pte));
588 __asm __volatile("itr.i itr[%0]=%1" :: "r"(1), "r"(pte));
589 __asm __volatile("mov psr.l=%0" :: "r" (psr));
594 map_gateway_page(void)
599 pte = PTE_PRESENT | PTE_MA_WB | PTE_ACCESSED | PTE_DIRTY |
600 PTE_PL_KERN | PTE_AR_X_RX;
601 pte |= (uint64_t)ia64_gateway_page & PTE_PPN_MASK;
603 __asm __volatile("ptr.d %0,%1; ptr.i %0,%1" ::
604 "r"(VM_MAX_ADDRESS), "r"(PAGE_SHIFT << 2));
606 __asm __volatile("mov %0=psr" : "=r"(psr));
607 __asm __volatile("rsm psr.ic|psr.i");
609 ia64_set_ifa(VM_MAX_ADDRESS);
610 ia64_set_itir(PAGE_SHIFT << 2);
612 __asm __volatile("itr.d dtr[%0]=%1" :: "r"(3), "r"(pte));
614 __asm __volatile("itr.i itr[%0]=%1" :: "r"(3), "r"(pte));
615 __asm __volatile("mov psr.l=%0" :: "r" (psr));
618 /* Expose the mapping to userland in ar.k5 */
619 ia64_set_k5(VM_MAX_ADDRESS);
623 freq_ratio(u_long base, u_long ratio)
627 f = (base * (ratio >> 32)) / (ratio & 0xfffffffful);
628 return ((f + 500000) / 1000000);
632 calculate_frequencies(void)
634 struct ia64_sal_result sal;
635 struct ia64_pal_result pal;
637 sal = ia64_sal_entry(SAL_FREQ_BASE, 0, 0, 0, 0, 0, 0, 0);
638 pal = ia64_call_pal_static(PAL_FREQ_RATIOS, 0, 0, 0);
640 if (sal.sal_status == 0 && pal.pal_status == 0) {
642 printf("Platform clock frequency %ld Hz\n",
644 printf("Processor ratio %ld/%ld, Bus ratio %ld/%ld, "
645 "ITC ratio %ld/%ld\n",
646 pal.pal_result[0] >> 32,
647 pal.pal_result[0] & ((1L << 32) - 1),
648 pal.pal_result[1] >> 32,
649 pal.pal_result[1] & ((1L << 32) - 1),
650 pal.pal_result[2] >> 32,
651 pal.pal_result[2] & ((1L << 32) - 1));
653 cpu_freq = freq_ratio(sal.sal_result[0], pal.pal_result[0]);
654 bus_freq = freq_ratio(sal.sal_result[0], pal.pal_result[1]);
655 itc_freq = freq_ratio(sal.sal_result[0], pal.pal_result[2]);
659 struct ia64_init_return
662 struct ia64_init_return ret;
664 vm_offset_t kernstart, kernend;
665 vm_offset_t kernstartpfn, kernendpfn, pfn0, pfn1;
668 int metadata_missing;
670 /* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
673 * TODO: Disable interrupts, floating point etc.
674 * Maybe flush cache and tlb
676 ia64_set_fpsr(IA64_FPSR_DEFAULT);
679 * TODO: Get critical system information (if possible, from the
680 * information provided by the boot program).
684 * pa_bootinfo is the physical address of the bootinfo block as
685 * passed to us by the loader and set in locore.s.
687 bootinfo = *(struct bootinfo *)(IA64_PHYS_TO_RR7(pa_bootinfo));
689 if (bootinfo.bi_magic != BOOTINFO_MAGIC || bootinfo.bi_version != 1) {
690 bzero(&bootinfo, sizeof(bootinfo));
691 bootinfo.bi_kernend = (vm_offset_t) round_page(_end);
695 * Look for the I/O ports first - we need them for console
698 for (md = efi_md_first(); md != NULL; md = efi_md_next(md)) {
699 switch (md->md_type) {
700 case EFI_MD_TYPE_IOPORT:
701 ia64_port_base = (uintptr_t)pmap_mapdev(md->md_phys,
702 md->md_pages * EFI_PAGE_SIZE);
704 case EFI_MD_TYPE_PALCODE:
705 ia64_pal_base = md->md_phys;
710 metadata_missing = 0;
711 if (bootinfo.bi_modulep)
712 preload_metadata = (caddr_t)bootinfo.bi_modulep;
714 metadata_missing = 1;
716 if (envmode == 0 && bootinfo.bi_envp)
717 kern_envp = (caddr_t)bootinfo.bi_envp;
719 kern_envp = static_env;
722 * Look at arguments passed to us and compute boothowto.
724 boothowto = bootinfo.bi_boothowto;
726 if (boothowto & RB_VERBOSE)
730 * Find the beginning and end of the kernel.
732 kernstart = trunc_page(kernel_text);
734 ksym_start = bootinfo.bi_symtab;
735 ksym_end = bootinfo.bi_esymtab;
736 kernend = (vm_offset_t)round_page(ksym_end);
738 kernend = (vm_offset_t)round_page(_end);
740 /* But if the bootstrap tells us otherwise, believe it! */
741 if (bootinfo.bi_kernend)
742 kernend = round_page(bootinfo.bi_kernend);
745 * Setup the PCPU data for the bootstrap processor. It is needed
746 * by printf(). Also, since printf() has critical sections, we
747 * need to initialize at least pc_curthread.
750 ia64_set_k4((u_int64_t)pcpup);
751 pcpu_init(pcpup, 0, sizeof(pcpu0));
752 dpcpu_init((void *)kernend, 0);
753 kernend += DPCPU_SIZE;
754 PCPU_SET(curthread, &thread0);
757 * Initialize the console before we print anything out.
761 /* OUTPUT NOW ALLOWED */
763 if (ia64_pal_base != 0) {
764 ia64_pal_base &= ~IA64_ID_PAGE_MASK;
766 * We use a TR to map the first 256M of memory - this might
767 * cover the palcode too.
769 if (ia64_pal_base == 0)
770 printf("PAL code mapped by the kernel's TR\n");
772 printf("PAL code not found\n");
775 * Wire things up so we can call the firmware.
778 efi_boot_minimal(bootinfo.bi_systab);
781 calculate_frequencies();
783 if (metadata_missing)
784 printf("WARNING: loader(8) metadata is missing!\n");
786 /* Get FPSWA interface */
787 fpswa_iface = (bootinfo.bi_fpswa == 0) ? NULL :
788 (struct fpswa_iface *)IA64_PHYS_TO_RR7(bootinfo.bi_fpswa);
790 /* Init basic tunables, including hz */
793 p = getenv("kernelname");
795 strncpy(kernelname, p, sizeof(kernelname) - 1);
799 kernstartpfn = atop(IA64_RR_MASK(kernstart));
800 kernendpfn = atop(IA64_RR_MASK(kernend));
803 * Size the memory regions and load phys_avail[] with the results.
807 * Find out how much memory is available, by looking at
808 * the memory descriptors.
812 printf("Memory descriptor count: %d\n", mdcount);
816 for (md = efi_md_first(); md != NULL; md = efi_md_next(md)) {
818 printf("MD %p: type %d pa 0x%lx cnt 0x%lx\n", md,
819 md->md_type, md->md_phys, md->md_pages);
822 pfn0 = ia64_btop(round_page(md->md_phys));
823 pfn1 = ia64_btop(trunc_page(md->md_phys + md->md_pages * 4096));
827 if (md->md_type != EFI_MD_TYPE_FREE)
831 * We have a memory descriptor that describes conventional
832 * memory that is for general use. We must determine if the
833 * loader has put the kernel in this region.
835 physmem += (pfn1 - pfn0);
836 if (pfn0 <= kernendpfn && kernstartpfn <= pfn1) {
838 * Must compute the location of the kernel
839 * within the segment.
842 printf("Descriptor %p contains kernel\n", mp);
844 if (pfn0 < kernstartpfn) {
846 * There is a chunk before the kernel.
849 printf("Loading chunk before kernel: "
850 "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
852 phys_avail[phys_avail_cnt] = ia64_ptob(pfn0);
853 phys_avail[phys_avail_cnt+1] = ia64_ptob(kernstartpfn);
856 if (kernendpfn < pfn1) {
858 * There is a chunk after the kernel.
861 printf("Loading chunk after kernel: "
862 "0x%lx / 0x%lx\n", kernendpfn, pfn1);
864 phys_avail[phys_avail_cnt] = ia64_ptob(kernendpfn);
865 phys_avail[phys_avail_cnt+1] = ia64_ptob(pfn1);
870 * Just load this cluster as one chunk.
873 printf("Loading descriptor %d: 0x%lx / 0x%lx\n", i,
876 phys_avail[phys_avail_cnt] = ia64_ptob(pfn0);
877 phys_avail[phys_avail_cnt+1] = ia64_ptob(pfn1);
882 phys_avail[phys_avail_cnt] = 0;
885 init_param2(physmem);
888 * Initialize error message buffer (at end of core).
890 msgbufp = (struct msgbuf *)pmap_steal_memory(msgbufsize);
891 msgbufinit(msgbufp, msgbufsize);
893 proc_linkup0(&proc0, &thread0);
895 * Init mapping for kernel stack for proc 0
897 thread0.td_kstack = pmap_steal_memory(KSTACK_PAGES * PAGE_SIZE);
898 thread0.td_kstack_pages = KSTACK_PAGES;
903 * Initialize the rest of proc 0's PCB.
905 * Set the kernel sp, reserving space for an (empty) trapframe,
906 * and make proc0's trapframe pointer point to it for sanity.
907 * Initialise proc0's backing store to start after u area.
909 cpu_thread_alloc(&thread0);
910 thread0.td_frame->tf_flags = FRAME_SYSCALL;
911 thread0.td_pcb->pcb_special.sp =
912 (u_int64_t)thread0.td_frame - 16;
913 thread0.td_pcb->pcb_special.bspstore = thread0.td_kstack;
916 * Initialize the virtual memory system.
921 * Initialize debuggers, and break into them if appropriate.
926 if (boothowto & RB_KDB)
927 kdb_enter(KDB_WHY_BOOTFLAGS,
928 "Boot flags requested debugger\n");
934 ret.bspstore = thread0.td_pcb->pcb_special.bspstore;
935 ret.sp = thread0.td_pcb->pcb_special.sp;
943 return (bootinfo.bi_hcdp);
947 bzero(void *buf, size_t len)
951 while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
955 while (len >= sizeof(u_long) * 8) {
957 *((u_long*) p + 1) = 0;
958 *((u_long*) p + 2) = 0;
959 *((u_long*) p + 3) = 0;
960 len -= sizeof(u_long) * 8;
961 *((u_long*) p + 4) = 0;
962 *((u_long*) p + 5) = 0;
963 *((u_long*) p + 6) = 0;
964 *((u_long*) p + 7) = 0;
965 p += sizeof(u_long) * 8;
967 while (len >= sizeof(u_long)) {
969 len -= sizeof(u_long);
988 u_int64_t start, end, now;
992 start = ia64_get_itc();
993 end = start + itc_freq * n;
994 /* printf("DELAY from 0x%lx to 0x%lx\n", start, end); */
996 now = ia64_get_itc();
997 } while (now < end || (now > start && end < start));
1003 * Send an interrupt (signal) to a process.
1006 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
1010 struct trapframe *tf;
1011 struct sigacts *psp;
1012 struct sigframe sf, *sfp;
1020 PROC_LOCK_ASSERT(p, MA_OWNED);
1021 sig = ksi->ksi_signo;
1022 code = ksi->ksi_code;
1024 mtx_assert(&psp->ps_mtx, MA_OWNED);
1026 sp = tf->tf_special.sp;
1027 oonstack = sigonstack(sp);
1030 /* save user context */
1031 bzero(&sf, sizeof(struct sigframe));
1032 sf.sf_uc.uc_sigmask = *mask;
1033 sf.sf_uc.uc_stack = td->td_sigstk;
1034 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
1035 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1038 * Allocate and validate space for the signal handler
1039 * context. Note that if the stack is in P0 space, the
1040 * call to grow() is a nop, and the useracc() check
1041 * will fail if the process has not already allocated
1042 * the space with a `brk'.
1044 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
1045 SIGISMEMBER(psp->ps_sigonstack, sig)) {
1046 sbs = (u_int64_t)td->td_sigstk.ss_sp;
1047 sbs = (sbs + 15) & ~15;
1048 sfp = (struct sigframe *)(sbs + td->td_sigstk.ss_size);
1049 #if defined(COMPAT_43)
1050 td->td_sigstk.ss_flags |= SS_ONSTACK;
1053 sfp = (struct sigframe *)sp;
1054 sfp = (struct sigframe *)((u_int64_t)(sfp - 1) & ~15);
1056 /* Fill in the siginfo structure for POSIX handlers. */
1057 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
1058 sf.sf_si = ksi->ksi_info;
1059 sf.sf_si.si_signo = sig;
1061 * XXX this shouldn't be here after code in trap.c
1064 sf.sf_si.si_addr = (void*)tf->tf_special.ifa;
1065 code = (u_int64_t)&sfp->sf_si;
1068 mtx_unlock(&psp->ps_mtx);
1071 get_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
1073 /* Copy the frame out to userland. */
1074 if (copyout(&sf, sfp, sizeof(sf)) != 0) {
1076 * Process has trashed its stack; give it an illegal
1077 * instruction to halt it in its tracks.
1080 sigexit(td, SIGILL);
1084 if ((tf->tf_flags & FRAME_SYSCALL) == 0) {
1085 tf->tf_special.psr &= ~IA64_PSR_RI;
1086 tf->tf_special.iip = ia64_get_k5() +
1087 ((uint64_t)break_sigtramp - (uint64_t)ia64_gateway_page);
1089 tf->tf_special.iip = ia64_get_k5() +
1090 ((uint64_t)epc_sigtramp - (uint64_t)ia64_gateway_page);
1093 * Setup the trapframe to return to the signal trampoline. We pass
1094 * information to the trampoline in the following registers:
1096 * gp new backing store or NULL
1098 * r9 signal code or siginfo pointer
1099 * r10 signal handler (function descriptor)
1101 tf->tf_special.sp = (u_int64_t)sfp - 16;
1102 tf->tf_special.gp = sbs;
1103 tf->tf_special.bspstore = sf.sf_uc.uc_mcontext.mc_special.bspstore;
1104 tf->tf_special.ndirty = 0;
1105 tf->tf_special.rnat = sf.sf_uc.uc_mcontext.mc_special.rnat;
1106 tf->tf_scratch.gr8 = sig;
1107 tf->tf_scratch.gr9 = code;
1108 tf->tf_scratch.gr10 = (u_int64_t)catcher;
1111 mtx_lock(&psp->ps_mtx);
1115 * System call to cleanup state after a signal
1116 * has been taken. Reset signal mask and
1117 * stack state from context left by sendsig (above).
1118 * Return to previous pc and psl as specified by
1119 * context left by sendsig. Check carefully to
1120 * make sure that the user has not modified the
1121 * state to gain improper privileges.
1126 sigreturn(struct thread *td,
1127 struct sigreturn_args /* {
1128 ucontext_t *sigcntxp;
1132 struct trapframe *tf;
1139 * Fetch the entire context structure at once for speed.
1140 * We don't use a normal argument to simplify RSE handling.
1142 if (copyin(uap->sigcntxp, (caddr_t)&uc, sizeof(uc)))
1145 set_mcontext(td, &uc.uc_mcontext);
1147 #if defined(COMPAT_43)
1148 if (sigonstack(tf->tf_special.sp))
1149 td->td_sigstk.ss_flags |= SS_ONSTACK;
1151 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
1153 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
1155 return (EJUSTRETURN);
1158 #ifdef COMPAT_FREEBSD4
1160 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
1163 return sigreturn(td, (struct sigreturn_args *)uap);
1168 * Construct a PCB from a trapframe. This is called from kdb_trap() where
1169 * we want to start a backtrace from the function that caused us to enter
1170 * the debugger. We have the context in the trapframe, but base the trace
1171 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
1172 * enough for a backtrace.
1175 makectx(struct trapframe *tf, struct pcb *pcb)
1178 pcb->pcb_special = tf->tf_special;
1179 pcb->pcb_special.__spare = ~0UL; /* XXX see unwind.c */
1180 save_callee_saved(&pcb->pcb_preserved);
1181 save_callee_saved_fp(&pcb->pcb_preserved_fp);
1185 ia64_flush_dirty(struct thread *td, struct _special *r)
1189 uint64_t bspst, kstk, rnat;
1195 kstk = td->td_kstack + (r->bspstore & 0x1ffUL);
1196 if (td == curthread) {
1197 __asm __volatile("mov ar.rsc=0;;");
1198 __asm __volatile("mov %0=ar.bspstore" : "=r"(bspst));
1199 /* Make sure we have all the user registers written out. */
1200 if (bspst - kstk < r->ndirty) {
1201 __asm __volatile("flushrs;;");
1202 __asm __volatile("mov %0=ar.bspstore" : "=r"(bspst));
1204 __asm __volatile("mov %0=ar.rnat;;" : "=r"(rnat));
1205 __asm __volatile("mov ar.rsc=3");
1206 error = copyout((void*)kstk, (void*)r->bspstore, r->ndirty);
1208 r->rnat = (bspst > kstk && (bspst & 0x1ffL) < (kstk & 0x1ffL))
1209 ? *(uint64_t*)(kstk | 0x1f8L) : rnat;
1211 locked = PROC_LOCKED(td->td_proc);
1214 iov.iov_base = (void*)(uintptr_t)kstk;
1215 iov.iov_len = r->ndirty;
1218 uio.uio_offset = r->bspstore;
1219 uio.uio_resid = r->ndirty;
1220 uio.uio_segflg = UIO_SYSSPACE;
1221 uio.uio_rw = UIO_WRITE;
1223 error = proc_rwmem(td->td_proc, &uio);
1225 * XXX proc_rwmem() doesn't currently return ENOSPC,
1226 * so I think it can bogusly return 0. Neither do
1227 * we allow short writes.
1229 if (uio.uio_resid != 0 && error == 0)
1235 r->bspstore += r->ndirty;
1241 get_mcontext(struct thread *td, mcontext_t *mc, int flags)
1243 struct trapframe *tf;
1247 bzero(mc, sizeof(*mc));
1248 mc->mc_special = tf->tf_special;
1249 error = ia64_flush_dirty(td, &mc->mc_special);
1250 if (tf->tf_flags & FRAME_SYSCALL) {
1251 mc->mc_flags |= _MC_FLAGS_SYSCALL_CONTEXT;
1252 mc->mc_scratch = tf->tf_scratch;
1253 if (flags & GET_MC_CLEAR_RET) {
1254 mc->mc_scratch.gr8 = 0;
1255 mc->mc_scratch.gr9 = 0;
1256 mc->mc_scratch.gr10 = 0;
1257 mc->mc_scratch.gr11 = 0;
1260 mc->mc_flags |= _MC_FLAGS_ASYNC_CONTEXT;
1261 mc->mc_scratch = tf->tf_scratch;
1262 mc->mc_scratch_fp = tf->tf_scratch_fp;
1264 * XXX If the thread never used the high FP registers, we
1265 * probably shouldn't waste time saving them.
1267 ia64_highfp_save(td);
1268 mc->mc_flags |= _MC_FLAGS_HIGHFP_VALID;
1269 mc->mc_high_fp = td->td_pcb->pcb_high_fp;
1271 save_callee_saved(&mc->mc_preserved);
1272 save_callee_saved_fp(&mc->mc_preserved_fp);
1277 set_mcontext(struct thread *td, const mcontext_t *mc)
1280 struct trapframe *tf;
1285 KASSERT((tf->tf_special.ndirty & ~PAGE_MASK) == 0,
1286 ("Whoa there! We have more than 8KB of dirty registers!"));
1290 * Only copy the user mask and the restart instruction bit from
1293 psrmask = IA64_PSR_BE | IA64_PSR_UP | IA64_PSR_AC | IA64_PSR_MFL |
1294 IA64_PSR_MFH | IA64_PSR_RI;
1295 s.psr = (tf->tf_special.psr & ~psrmask) | (s.psr & psrmask);
1296 /* We don't have any dirty registers of the new context. */
1298 if (mc->mc_flags & _MC_FLAGS_ASYNC_CONTEXT) {
1300 * We can get an async context passed to us while we
1301 * entered the kernel through a syscall: sigreturn(2)
1302 * takes contexts that could previously be the result of
1303 * a trap or interrupt.
1304 * Hence, we cannot assert that the trapframe is not
1305 * a syscall frame, but we can assert that it's at
1306 * least an expected syscall.
1308 if (tf->tf_flags & FRAME_SYSCALL) {
1309 KASSERT(tf->tf_scratch.gr15 == SYS_sigreturn, ("foo"));
1310 tf->tf_flags &= ~FRAME_SYSCALL;
1312 tf->tf_scratch = mc->mc_scratch;
1313 tf->tf_scratch_fp = mc->mc_scratch_fp;
1314 if (mc->mc_flags & _MC_FLAGS_HIGHFP_VALID)
1315 td->td_pcb->pcb_high_fp = mc->mc_high_fp;
1317 KASSERT((tf->tf_flags & FRAME_SYSCALL) != 0, ("foo"));
1318 if ((mc->mc_flags & _MC_FLAGS_SYSCALL_CONTEXT) == 0) {
1319 s.cfm = tf->tf_special.cfm;
1320 s.iip = tf->tf_special.iip;
1321 tf->tf_scratch.gr15 = 0; /* Clear syscall nr. */
1323 tf->tf_scratch = mc->mc_scratch;
1326 restore_callee_saved(&mc->mc_preserved);
1327 restore_callee_saved_fp(&mc->mc_preserved_fp);
1333 * Clear registers on exec.
1336 exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
1338 struct trapframe *tf;
1339 uint64_t *ksttop, *kst;
1342 ksttop = (uint64_t*)(td->td_kstack + tf->tf_special.ndirty +
1343 (tf->tf_special.bspstore & 0x1ffUL));
1346 * We can ignore up to 8KB of dirty registers by masking off the
1347 * lower 13 bits in exception_restore() or epc_syscall(). This
1348 * should be enough for a couple of years, but if there are more
1349 * than 8KB of dirty registers, we lose track of the bottom of
1350 * the kernel stack. The solution is to copy the active part of
1351 * the kernel stack down 1 page (or 2, but not more than that)
1352 * so that we always have less than 8KB of dirty registers.
1354 KASSERT((tf->tf_special.ndirty & ~PAGE_MASK) == 0,
1355 ("Whoa there! We have more than 8KB of dirty registers!"));
1357 bzero(&tf->tf_special, sizeof(tf->tf_special));
1358 if ((tf->tf_flags & FRAME_SYSCALL) == 0) { /* break syscalls. */
1359 bzero(&tf->tf_scratch, sizeof(tf->tf_scratch));
1360 bzero(&tf->tf_scratch_fp, sizeof(tf->tf_scratch_fp));
1361 tf->tf_special.cfm = (1UL<<63) | (3UL<<7) | 3UL;
1362 tf->tf_special.bspstore = IA64_BACKINGSTORE;
1364 * Copy the arguments onto the kernel register stack so that
1365 * they get loaded by the loadrs instruction. Skip over the
1366 * NaT collection points.
1369 if (((uintptr_t)kst & 0x1ff) == 0x1f8)
1372 if (((uintptr_t)kst & 0x1ff) == 0x1f8)
1374 *kst-- = ps_strings;
1375 if (((uintptr_t)kst & 0x1ff) == 0x1f8)
1378 tf->tf_special.ndirty = (ksttop - kst) << 3;
1379 } else { /* epc syscalls (default). */
1380 tf->tf_special.cfm = (3UL<<62) | (3UL<<7) | 3UL;
1381 tf->tf_special.bspstore = IA64_BACKINGSTORE + 24;
1383 * Write values for out0, out1 and out2 to the user's backing
1384 * store and arrange for them to be restored into the user's
1385 * initial register frame.
1386 * Assumes that (bspstore & 0x1f8) < 0x1e0.
1388 suword((caddr_t)tf->tf_special.bspstore - 24, stack);
1389 suword((caddr_t)tf->tf_special.bspstore - 16, ps_strings);
1390 suword((caddr_t)tf->tf_special.bspstore - 8, 0);
1393 tf->tf_special.iip = entry;
1394 tf->tf_special.sp = (stack & ~15) - 16;
1395 tf->tf_special.rsc = 0xf;
1396 tf->tf_special.fpsr = IA64_FPSR_DEFAULT;
1397 tf->tf_special.psr = IA64_PSR_IC | IA64_PSR_I | IA64_PSR_IT |
1398 IA64_PSR_DT | IA64_PSR_RT | IA64_PSR_DFH | IA64_PSR_BN |
1403 ptrace_set_pc(struct thread *td, unsigned long addr)
1407 switch (addr & 0xFUL) {
1409 slot = IA64_PSR_RI_0;
1412 /* XXX we need to deal with MLX bundles here */
1413 slot = IA64_PSR_RI_1;
1416 slot = IA64_PSR_RI_2;
1422 td->td_frame->tf_special.iip = addr & ~0x0FULL;
1423 td->td_frame->tf_special.psr =
1424 (td->td_frame->tf_special.psr & ~IA64_PSR_RI) | slot;
1429 ptrace_single_step(struct thread *td)
1431 struct trapframe *tf;
1434 * There's no way to set single stepping when we're leaving the
1435 * kernel through the EPC syscall path. The way we solve this is
1436 * by enabling the lower-privilege trap so that we re-enter the
1437 * kernel as soon as the privilege level changes. See trap.c for
1438 * how we proceed from there.
1441 if (tf->tf_flags & FRAME_SYSCALL)
1442 tf->tf_special.psr |= IA64_PSR_LP;
1444 tf->tf_special.psr |= IA64_PSR_SS;
1449 ptrace_clear_single_step(struct thread *td)
1451 struct trapframe *tf;
1454 * Clear any and all status bits we may use to implement single
1458 tf->tf_special.psr &= ~IA64_PSR_SS;
1459 tf->tf_special.psr &= ~IA64_PSR_LP;
1460 tf->tf_special.psr &= ~IA64_PSR_TB;
1465 fill_regs(struct thread *td, struct reg *regs)
1467 struct trapframe *tf;
1470 regs->r_special = tf->tf_special;
1471 regs->r_scratch = tf->tf_scratch;
1472 save_callee_saved(®s->r_preserved);
1477 set_regs(struct thread *td, struct reg *regs)
1479 struct trapframe *tf;
1483 error = ia64_flush_dirty(td, &tf->tf_special);
1485 tf->tf_special = regs->r_special;
1486 tf->tf_special.bspstore += tf->tf_special.ndirty;
1487 tf->tf_special.ndirty = 0;
1488 tf->tf_scratch = regs->r_scratch;
1489 restore_callee_saved(®s->r_preserved);
1495 fill_dbregs(struct thread *td, struct dbreg *dbregs)
1502 set_dbregs(struct thread *td, struct dbreg *dbregs)
1509 fill_fpregs(struct thread *td, struct fpreg *fpregs)
1511 struct trapframe *frame = td->td_frame;
1512 struct pcb *pcb = td->td_pcb;
1514 /* Save the high FP registers. */
1515 ia64_highfp_save(td);
1517 fpregs->fpr_scratch = frame->tf_scratch_fp;
1518 save_callee_saved_fp(&fpregs->fpr_preserved);
1519 fpregs->fpr_high = pcb->pcb_high_fp;
1524 set_fpregs(struct thread *td, struct fpreg *fpregs)
1526 struct trapframe *frame = td->td_frame;
1527 struct pcb *pcb = td->td_pcb;
1529 /* Throw away the high FP registers (should be redundant). */
1530 ia64_highfp_drop(td);
1532 frame->tf_scratch_fp = fpregs->fpr_scratch;
1533 restore_callee_saved_fp(&fpregs->fpr_preserved);
1534 pcb->pcb_high_fp = fpregs->fpr_high;
1539 ia64_sync_icache(vm_offset_t va, vm_offset_t sz)
1543 if (!ia64_sync_icache_needed)