machdep.c revision 1.220 1 /* $NetBSD: machdep.c,v 1.220 2000/09/13 15:00:15 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center and by Chris G. Demetriou.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
42 * All rights reserved.
43 *
44 * Author: Chris G. Demetriou
45 *
46 * Permission to use, copy, modify and distribute this software and
47 * its documentation is hereby granted, provided that both the copyright
48 * notice and this permission notice appear in all copies of the
49 * software, derivative works or modified versions, and any portions
50 * thereof, and that both notices appear in supporting documentation.
51 *
52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55 *
56 * Carnegie Mellon requests users of this software to return to
57 *
58 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
59 * School of Computer Science
60 * Carnegie Mellon University
61 * Pittsburgh PA 15213-3890
62 *
63 * any improvements or extensions that they make and grant Carnegie the
64 * rights to redistribute these changes.
65 */
66
67 #include "opt_ddb.h"
68 #include "opt_multiprocessor.h"
69 #include "opt_dec_3000_300.h"
70 #include "opt_dec_3000_500.h"
71 #include "opt_compat_osf1.h"
72 #include "opt_compat_netbsd.h"
73
74 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
75
76 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.220 2000/09/13 15:00:15 thorpej Exp $");
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/signalvar.h>
81 #include <sys/kernel.h>
82 #include <sys/map.h>
83 #include <sys/proc.h>
84 #include <sys/sched.h>
85 #include <sys/buf.h>
86 #include <sys/reboot.h>
87 #include <sys/device.h>
88 #include <sys/file.h>
89 #include <sys/malloc.h>
90 #include <sys/mbuf.h>
91 #include <sys/mman.h>
92 #include <sys/msgbuf.h>
93 #include <sys/ioctl.h>
94 #include <sys/tty.h>
95 #include <sys/user.h>
96 #include <sys/exec.h>
97 #include <sys/exec_ecoff.h>
98 #include <sys/core.h>
99 #include <sys/kcore.h>
100 #include <machine/kcore.h>
101
102 #include <sys/mount.h>
103 #include <sys/syscallargs.h>
104
105 #include <uvm/uvm_extern.h>
106 #include <sys/sysctl.h>
107
108 #include <dev/cons.h>
109
110 #include <machine/autoconf.h>
111 #include <machine/cpu.h>
112 #include <machine/reg.h>
113 #include <machine/rpb.h>
114 #include <machine/prom.h>
115 #include <machine/conf.h>
116 #include <machine/ieeefp.h>
117
118 #ifdef DDB
119 #include <machine/db_machdep.h>
120 #include <ddb/db_access.h>
121 #include <ddb/db_sym.h>
122 #include <ddb/db_extern.h>
123 #include <ddb/db_interface.h>
124 #endif
125
126 #include <machine/alpha.h>
127
128 vm_map_t exec_map = NULL;
129 vm_map_t mb_map = NULL;
130 vm_map_t phys_map = NULL;
131
132 caddr_t msgbufaddr;
133
134 int maxmem; /* max memory per process */
135
136 int totalphysmem; /* total amount of physical memory in system */
137 int physmem; /* physical memory used by NetBSD + some rsvd */
138 int resvmem; /* amount of memory reserved for PROM */
139 int unusedmem; /* amount of memory for OS that we don't use */
140 int unknownmem; /* amount of memory with an unknown use */
141
142 int cputype; /* system type, from the RPB */
143
144 int bootdev_debug = 0; /* patchable, or from DDB */
145
146 /*
147 * XXX We need an address to which we can assign things so that they
148 * won't be optimized away because we didn't use the value.
149 */
150 u_int32_t no_optimize;
151
152 /* the following is used externally (sysctl_hw) */
153 char machine[] = MACHINE; /* from <machine/param.h> */
154 char machine_arch[] = MACHINE_ARCH; /* from <machine/param.h> */
155 char cpu_model[128];
156
157 struct user *proc0paddr;
158
159 /* Number of machine cycles per microsecond */
160 u_int64_t cycles_per_usec;
161
162 /* number of cpus in the box. really! */
163 int ncpus;
164
165 struct bootinfo_kernel bootinfo;
166
167 /* For built-in TCDS */
168 #if defined(DEC_3000_300) || defined(DEC_3000_500)
169 u_int8_t dec_3000_scsiid[2], dec_3000_scsifast[2];
170 #endif
171
172 struct platform platform;
173
174 #ifdef DDB
175 /* start and end of kernel symbol table */
176 void *ksym_start, *ksym_end;
177 #endif
178
179 /* for cpu_sysctl() */
180 int alpha_unaligned_print = 1; /* warn about unaligned accesses */
181 int alpha_unaligned_fix = 1; /* fix up unaligned accesses */
182 int alpha_unaligned_sigbus = 0; /* don't SIGBUS on fixed-up accesses */
183
184 /*
185 * XXX This should be dynamically sized, but we have the chicken-egg problem!
186 * XXX it should also be larger than it is, because not all of the mddt
187 * XXX clusters end up being used for VM.
188 */
189 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX]; /* low size bits overloaded */
190 int mem_cluster_cnt;
191
192 int cpu_dump __P((void));
193 int cpu_dumpsize __P((void));
194 u_long cpu_dump_mempagecnt __P((void));
195 void dumpsys __P((void));
196 void identifycpu __P((void));
197 void printregs __P((struct reg *));
198
199 void
200 alpha_init(pfn, ptb, bim, bip, biv)
201 u_long pfn; /* first free PFN number */
202 u_long ptb; /* PFN of current level 1 page table */
203 u_long bim; /* bootinfo magic */
204 u_long bip; /* bootinfo pointer */
205 u_long biv; /* bootinfo version */
206 {
207 extern char kernel_text[], _end[];
208 struct mddt *mddtp;
209 struct mddt_cluster *memc;
210 int i, mddtweird;
211 struct vm_physseg *vps;
212 vaddr_t kernstart, kernend;
213 paddr_t kernstartpfn, kernendpfn, pfn0, pfn1;
214 vsize_t size;
215 cpuid_t cpu_id;
216 struct cpu_info *ci;
217 char *p;
218 caddr_t v;
219 const char *bootinfo_msg;
220 const struct cpuinit *c;
221
222 /* NO OUTPUT ALLOWED UNTIL FURTHER NOTICE */
223
224 /*
225 * Turn off interrupts (not mchecks) and floating point.
226 * Make sure the instruction and data streams are consistent.
227 */
228 (void)alpha_pal_swpipl(ALPHA_PSL_IPL_HIGH);
229 alpha_pal_wrfen(0);
230 ALPHA_TBIA();
231 alpha_pal_imb();
232
233 cpu_id = cpu_number();
234
235 #if defined(MULTIPROCESSOR)
236 /*
237 * Set our SysValue to the address of our cpu_info structure.
238 * Secondary processors do this in their spinup trampoline.
239 */
240 alpha_pal_wrval((u_long)&cpu_info[cpu_id]);
241 #endif
242
243 ci = curcpu();
244 ci->ci_cpuid = cpu_id;
245
246 /*
247 * Get critical system information (if possible, from the
248 * information provided by the boot program).
249 */
250 bootinfo_msg = NULL;
251 if (bim == BOOTINFO_MAGIC) {
252 if (biv == 0) { /* backward compat */
253 biv = *(u_long *)bip;
254 bip += 8;
255 }
256 switch (biv) {
257 case 1: {
258 struct bootinfo_v1 *v1p = (struct bootinfo_v1 *)bip;
259
260 bootinfo.ssym = v1p->ssym;
261 bootinfo.esym = v1p->esym;
262 /* hwrpb may not be provided by boot block in v1 */
263 if (v1p->hwrpb != NULL) {
264 bootinfo.hwrpb_phys =
265 ((struct rpb *)v1p->hwrpb)->rpb_phys;
266 bootinfo.hwrpb_size = v1p->hwrpbsize;
267 } else {
268 bootinfo.hwrpb_phys =
269 ((struct rpb *)HWRPB_ADDR)->rpb_phys;
270 bootinfo.hwrpb_size =
271 ((struct rpb *)HWRPB_ADDR)->rpb_size;
272 }
273 bcopy(v1p->boot_flags, bootinfo.boot_flags,
274 min(sizeof v1p->boot_flags,
275 sizeof bootinfo.boot_flags));
276 bcopy(v1p->booted_kernel, bootinfo.booted_kernel,
277 min(sizeof v1p->booted_kernel,
278 sizeof bootinfo.booted_kernel));
279 /* booted dev not provided in bootinfo */
280 init_prom_interface((struct rpb *)
281 ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys));
282 prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
283 sizeof bootinfo.booted_dev);
284 break;
285 }
286 default:
287 bootinfo_msg = "unknown bootinfo version";
288 goto nobootinfo;
289 }
290 } else {
291 bootinfo_msg = "boot program did not pass bootinfo";
292 nobootinfo:
293 bootinfo.ssym = (u_long)_end;
294 bootinfo.esym = (u_long)_end;
295 bootinfo.hwrpb_phys = ((struct rpb *)HWRPB_ADDR)->rpb_phys;
296 bootinfo.hwrpb_size = ((struct rpb *)HWRPB_ADDR)->rpb_size;
297 init_prom_interface((struct rpb *)HWRPB_ADDR);
298 prom_getenv(PROM_E_BOOTED_OSFLAGS, bootinfo.boot_flags,
299 sizeof bootinfo.boot_flags);
300 prom_getenv(PROM_E_BOOTED_FILE, bootinfo.booted_kernel,
301 sizeof bootinfo.booted_kernel);
302 prom_getenv(PROM_E_BOOTED_DEV, bootinfo.booted_dev,
303 sizeof bootinfo.booted_dev);
304 }
305
306 /*
307 * Initialize the kernel's mapping of the RPB. It's needed for
308 * lots of things.
309 */
310 hwrpb = (struct rpb *)ALPHA_PHYS_TO_K0SEG(bootinfo.hwrpb_phys);
311
312 #if defined(DEC_3000_300) || defined(DEC_3000_500)
313 if (hwrpb->rpb_type == ST_DEC_3000_300 ||
314 hwrpb->rpb_type == ST_DEC_3000_500) {
315 prom_getenv(PROM_E_SCSIID, dec_3000_scsiid,
316 sizeof(dec_3000_scsiid));
317 prom_getenv(PROM_E_SCSIFAST, dec_3000_scsifast,
318 sizeof(dec_3000_scsifast));
319 }
320 #endif
321
322 /*
323 * Remember how many cycles there are per microsecond,
324 * so that we can use delay(). Round up, for safety.
325 */
326 cycles_per_usec = (hwrpb->rpb_cc_freq + 999999) / 1000000;
327
328 /*
329 * Initalize the (temporary) bootstrap console interface, so
330 * we can use printf until the VM system starts being setup.
331 * The real console is initialized before then.
332 */
333 init_bootstrap_console();
334
335 /* OUTPUT NOW ALLOWED */
336
337 /* delayed from above */
338 if (bootinfo_msg)
339 printf("WARNING: %s (0x%lx, 0x%lx, 0x%lx)\n",
340 bootinfo_msg, bim, bip, biv);
341
342 /* Initialize the trap vectors on the primary processor. */
343 trap_init();
344
345 /*
346 * Find out what hardware we're on, and do basic initialization.
347 */
348 cputype = hwrpb->rpb_type;
349 if (cputype < 0) {
350 /*
351 * At least some white-box systems have SRM which
352 * reports a systype that's the negative of their
353 * blue-box counterpart.
354 */
355 cputype = -cputype;
356 }
357 c = platform_lookup(cputype);
358 if (c == NULL) {
359 platform_not_supported();
360 /* NOTREACHED */
361 }
362 (*c->init)();
363 strcpy(cpu_model, platform.model);
364
365 /*
366 * Initalize the real console, so that the bootstrap console is
367 * no longer necessary.
368 */
369 (*platform.cons_init)();
370
371 #ifdef DIAGNOSTIC
372 /* Paranoid sanity checking */
373
374 /* We should always be running on the primary. */
375 assert(hwrpb->rpb_primary_cpu_id == cpu_id);
376
377 /*
378 * On single-CPU systypes, the primary should always be CPU 0,
379 * except on Alpha 8200 systems where the CPU id is related
380 * to the VID, which is related to the Turbo Laser node id.
381 */
382 if (cputype != ST_DEC_21000)
383 assert(hwrpb->rpb_primary_cpu_id == 0);
384 #endif
385
386 /* NO MORE FIRMWARE ACCESS ALLOWED */
387 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
388 /*
389 * XXX (unless _PMAP_MAY_USE_PROM_CONSOLE is defined and
390 * XXX pmap_uses_prom_console() evaluates to non-zero.)
391 */
392 #endif
393
394 /*
395 * find out this system's page size
396 */
397 PAGE_SIZE = hwrpb->rpb_page_size;
398 if (PAGE_SIZE != 8192)
399 panic("page size %d != 8192?!", PAGE_SIZE);
400
401 /*
402 * Initialize PAGE_SIZE-dependent variables.
403 */
404 uvm_setpagesize();
405
406 /*
407 * Find the beginning and end of the kernel (and leave a
408 * bit of space before the beginning for the bootstrap
409 * stack).
410 */
411 kernstart = trunc_page((vaddr_t)kernel_text) - 2 * PAGE_SIZE;
412 #ifdef DDB
413 ksym_start = (void *)bootinfo.ssym;
414 ksym_end = (void *)bootinfo.esym;
415 kernend = (vaddr_t)round_page((vaddr_t)ksym_end);
416 #else
417 kernend = (vaddr_t)round_page((vaddr_t)_end);
418 #endif
419
420 kernstartpfn = atop(ALPHA_K0SEG_TO_PHYS(kernstart));
421 kernendpfn = atop(ALPHA_K0SEG_TO_PHYS(kernend));
422
423 /*
424 * Find out how much memory is available, by looking at
425 * the memory cluster descriptors. This also tries to do
426 * its best to detect things things that have never been seen
427 * before...
428 */
429 mddtp = (struct mddt *)(((caddr_t)hwrpb) + hwrpb->rpb_memdat_off);
430
431 /* MDDT SANITY CHECKING */
432 mddtweird = 0;
433 if (mddtp->mddt_cluster_cnt < 2) {
434 mddtweird = 1;
435 printf("WARNING: weird number of mem clusters: %lu\n",
436 mddtp->mddt_cluster_cnt);
437 }
438
439 #if 0
440 printf("Memory cluster count: %d\n", mddtp->mddt_cluster_cnt);
441 #endif
442
443 for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
444 memc = &mddtp->mddt_clusters[i];
445 #if 0
446 printf("MEMC %d: pfn 0x%lx cnt 0x%lx usage 0x%lx\n", i,
447 memc->mddt_pfn, memc->mddt_pg_cnt, memc->mddt_usage);
448 #endif
449 totalphysmem += memc->mddt_pg_cnt;
450 if (mem_cluster_cnt < VM_PHYSSEG_MAX) { /* XXX */
451 mem_clusters[mem_cluster_cnt].start =
452 ptoa(memc->mddt_pfn);
453 mem_clusters[mem_cluster_cnt].size =
454 ptoa(memc->mddt_pg_cnt);
455 if (memc->mddt_usage & MDDT_mbz ||
456 memc->mddt_usage & MDDT_NONVOLATILE || /* XXX */
457 memc->mddt_usage & MDDT_PALCODE)
458 mem_clusters[mem_cluster_cnt].size |=
459 PROT_READ;
460 else
461 mem_clusters[mem_cluster_cnt].size |=
462 PROT_READ | PROT_WRITE | PROT_EXEC;
463 mem_cluster_cnt++;
464 }
465
466 if (memc->mddt_usage & MDDT_mbz) {
467 mddtweird = 1;
468 printf("WARNING: mem cluster %d has weird "
469 "usage 0x%lx\n", i, memc->mddt_usage);
470 unknownmem += memc->mddt_pg_cnt;
471 continue;
472 }
473 if (memc->mddt_usage & MDDT_NONVOLATILE) {
474 /* XXX should handle these... */
475 printf("WARNING: skipping non-volatile mem "
476 "cluster %d\n", i);
477 unusedmem += memc->mddt_pg_cnt;
478 continue;
479 }
480 if (memc->mddt_usage & MDDT_PALCODE) {
481 resvmem += memc->mddt_pg_cnt;
482 continue;
483 }
484
485 /*
486 * We have a memory cluster available for system
487 * software use. We must determine if this cluster
488 * holds the kernel.
489 */
490 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
491 /*
492 * XXX If the kernel uses the PROM console, we only use the
493 * XXX memory after the kernel in the first system segment,
494 * XXX to avoid clobbering prom mapping, data, etc.
495 */
496 if (!pmap_uses_prom_console() || physmem == 0) {
497 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
498 physmem += memc->mddt_pg_cnt;
499 pfn0 = memc->mddt_pfn;
500 pfn1 = memc->mddt_pfn + memc->mddt_pg_cnt;
501 if (pfn0 <= kernstartpfn && kernendpfn <= pfn1) {
502 /*
503 * Must compute the location of the kernel
504 * within the segment.
505 */
506 #if 0
507 printf("Cluster %d contains kernel\n", i);
508 #endif
509 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
510 if (!pmap_uses_prom_console()) {
511 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
512 if (pfn0 < kernstartpfn) {
513 /*
514 * There is a chunk before the kernel.
515 */
516 #if 0
517 printf("Loading chunk before kernel: "
518 "0x%lx / 0x%lx\n", pfn0, kernstartpfn);
519 #endif
520 uvm_page_physload(pfn0, kernstartpfn,
521 pfn0, kernstartpfn, VM_FREELIST_DEFAULT);
522 }
523 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
524 }
525 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
526 if (kernendpfn < pfn1) {
527 /*
528 * There is a chunk after the kernel.
529 */
530 #if 0
531 printf("Loading chunk after kernel: "
532 "0x%lx / 0x%lx\n", kernendpfn, pfn1);
533 #endif
534 uvm_page_physload(kernendpfn, pfn1,
535 kernendpfn, pfn1, VM_FREELIST_DEFAULT);
536 }
537 } else {
538 /*
539 * Just load this cluster as one chunk.
540 */
541 #if 0
542 printf("Loading cluster %d: 0x%lx / 0x%lx\n", i,
543 pfn0, pfn1);
544 #endif
545 uvm_page_physload(pfn0, pfn1, pfn0, pfn1,
546 VM_FREELIST_DEFAULT);
547 }
548 #ifdef _PMAP_MAY_USE_PROM_CONSOLE
549 }
550 #endif /* _PMAP_MAY_USE_PROM_CONSOLE */
551 }
552
553 /*
554 * Dump out the MDDT if it looks odd...
555 */
556 if (mddtweird) {
557 printf("\n");
558 printf("complete memory cluster information:\n");
559 for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
560 printf("mddt %d:\n", i);
561 printf("\tpfn %lx\n",
562 mddtp->mddt_clusters[i].mddt_pfn);
563 printf("\tcnt %lx\n",
564 mddtp->mddt_clusters[i].mddt_pg_cnt);
565 printf("\ttest %lx\n",
566 mddtp->mddt_clusters[i].mddt_pg_test);
567 printf("\tbva %lx\n",
568 mddtp->mddt_clusters[i].mddt_v_bitaddr);
569 printf("\tbpa %lx\n",
570 mddtp->mddt_clusters[i].mddt_p_bitaddr);
571 printf("\tbcksum %lx\n",
572 mddtp->mddt_clusters[i].mddt_bit_cksum);
573 printf("\tusage %lx\n",
574 mddtp->mddt_clusters[i].mddt_usage);
575 }
576 printf("\n");
577 }
578
579 if (totalphysmem == 0)
580 panic("can't happen: system seems to have no memory!");
581 maxmem = physmem;
582 #if 0
583 printf("totalphysmem = %d\n", totalphysmem);
584 printf("physmem = %d\n", physmem);
585 printf("resvmem = %d\n", resvmem);
586 printf("unusedmem = %d\n", unusedmem);
587 printf("unknownmem = %d\n", unknownmem);
588 #endif
589
590 /*
591 * Initialize error message buffer (at end of core).
592 */
593 {
594 vsize_t sz = (vsize_t)round_page(MSGBUFSIZE);
595 vsize_t reqsz = sz;
596
597 vps = &vm_physmem[vm_nphysseg - 1];
598
599 /* shrink so that it'll fit in the last segment */
600 if ((vps->avail_end - vps->avail_start) < atop(sz))
601 sz = ptoa(vps->avail_end - vps->avail_start);
602
603 vps->end -= atop(sz);
604 vps->avail_end -= atop(sz);
605 msgbufaddr = (caddr_t) ALPHA_PHYS_TO_K0SEG(ptoa(vps->end));
606 initmsgbuf(msgbufaddr, sz);
607
608 /* Remove the last segment if it now has no pages. */
609 if (vps->start == vps->end)
610 vm_nphysseg--;
611
612 /* warn if the message buffer had to be shrunk */
613 if (sz != reqsz)
614 printf("WARNING: %ld bytes not available for msgbuf "
615 "in last cluster (%ld used)\n", reqsz, sz);
616
617 }
618
619 /*
620 * Init mapping for u page(s) for proc 0
621 */
622 proc0.p_addr = proc0paddr =
623 (struct user *)pmap_steal_memory(UPAGES * PAGE_SIZE, NULL, NULL);
624
625 /*
626 * Allocate space for system data structures. These data structures
627 * are allocated here instead of cpu_startup() because physical
628 * memory is directly addressable. We don't have to map these into
629 * virtual address space.
630 */
631 size = (vsize_t)allocsys(NULL, NULL);
632 v = (caddr_t)pmap_steal_memory(size, NULL, NULL);
633 if ((allocsys(v, NULL) - v) != size)
634 panic("alpha_init: table size inconsistency");
635
636 /*
637 * Initialize the virtual memory system, and set the
638 * page table base register in proc 0's PCB.
639 */
640 pmap_bootstrap(ALPHA_PHYS_TO_K0SEG(ptb << PGSHIFT),
641 hwrpb->rpb_max_asn, hwrpb->rpb_pcs_cnt);
642
643 /*
644 * Initialize the rest of proc 0's PCB, and cache its physical
645 * address.
646 */
647 proc0.p_md.md_pcbpaddr =
648 (struct pcb *)ALPHA_K0SEG_TO_PHYS((vaddr_t)&proc0paddr->u_pcb);
649
650 /*
651 * Set the kernel sp, reserving space for an (empty) trapframe,
652 * and make proc0's trapframe pointer point to it for sanity.
653 */
654 proc0paddr->u_pcb.pcb_hw.apcb_ksp =
655 (u_int64_t)proc0paddr + USPACE - sizeof(struct trapframe);
656 proc0.p_md.md_tf =
657 (struct trapframe *)proc0paddr->u_pcb.pcb_hw.apcb_ksp;
658
659 /*
660 * Initialize the primary CPU's idle PCB to proc0's. In a
661 * MULTIPROCESSOR configuration, each CPU will later get
662 * its own idle PCB when autoconfiguration runs.
663 */
664 ci->ci_idle_pcb = &proc0paddr->u_pcb;
665 ci->ci_idle_pcb_paddr = (u_long)proc0.p_md.md_pcbpaddr;
666
667 /* Indicate that proc0 has a CPU. */
668 proc0.p_cpu = ci;
669
670 /*
671 * Look at arguments passed to us and compute boothowto.
672 */
673
674 boothowto = RB_SINGLE;
675 #ifdef KADB
676 boothowto |= RB_KDB;
677 #endif
678 for (p = bootinfo.boot_flags; p && *p != '\0'; p++) {
679 /*
680 * Note that we'd really like to differentiate case here,
681 * but the Alpha AXP Architecture Reference Manual
682 * says that we shouldn't.
683 */
684 switch (*p) {
685 case 'a': /* autoboot */
686 case 'A':
687 boothowto &= ~RB_SINGLE;
688 break;
689
690 #ifdef DEBUG
691 case 'c': /* crash dump immediately after autoconfig */
692 case 'C':
693 boothowto |= RB_DUMP;
694 break;
695 #endif
696
697 #if defined(KGDB) || defined(DDB)
698 case 'd': /* break into the kernel debugger ASAP */
699 case 'D':
700 boothowto |= RB_KDB;
701 break;
702 #endif
703
704 case 'h': /* always halt, never reboot */
705 case 'H':
706 boothowto |= RB_HALT;
707 break;
708
709 #if 0
710 case 'm': /* mini root present in memory */
711 case 'M':
712 boothowto |= RB_MINIROOT;
713 break;
714 #endif
715
716 case 'n': /* askname */
717 case 'N':
718 boothowto |= RB_ASKNAME;
719 break;
720
721 case 's': /* single-user (default, supported for sanity) */
722 case 'S':
723 boothowto |= RB_SINGLE;
724 break;
725
726 case '-':
727 /*
728 * Just ignore this. It's not required, but it's
729 * common for it to be passed regardless.
730 */
731 break;
732
733 default:
734 printf("Unrecognized boot flag '%c'.\n", *p);
735 break;
736 }
737 }
738
739
740 /*
741 * Figure out the number of cpus in the box, from RPB fields.
742 * Really. We mean it.
743 */
744 for (i = 0; i < hwrpb->rpb_pcs_cnt; i++) {
745 struct pcs *pcsp;
746
747 pcsp = LOCATE_PCS(hwrpb, i);
748 if ((pcsp->pcs_flags & PCS_PP) != 0)
749 ncpus++;
750 }
751
752 /*
753 * Initialize debuggers, and break into them if appropriate.
754 */
755 #ifdef DDB
756 db_machine_init();
757 ddb_init((int)((u_int64_t)ksym_end - (u_int64_t)ksym_start),
758 ksym_start, ksym_end);
759 if (boothowto & RB_KDB)
760 Debugger();
761 #endif
762 #ifdef KGDB
763 if (boothowto & RB_KDB)
764 kgdb_connect(0);
765 #endif
766 /*
767 * Figure out our clock frequency, from RPB fields.
768 */
769 hz = hwrpb->rpb_intr_freq >> 12;
770 if (!(60 <= hz && hz <= 10240)) {
771 hz = 1024;
772 #ifdef DIAGNOSTIC
773 printf("WARNING: unbelievable rpb_intr_freq: %ld (%d hz)\n",
774 hwrpb->rpb_intr_freq, hz);
775 #endif
776 }
777 }
778
779 void
780 consinit()
781 {
782
783 /*
784 * Everything related to console initialization is done
785 * in alpha_init().
786 */
787 #if defined(DIAGNOSTIC) && defined(_PMAP_MAY_USE_PROM_CONSOLE)
788 printf("consinit: %susing prom console\n",
789 pmap_uses_prom_console() ? "" : "not ");
790 #endif
791 }
792
793 #include "pckbc.h"
794 #include "pckbd.h"
795 #if (NPCKBC > 0) && (NPCKBD == 0)
796
797 #include <dev/ic/pckbcvar.h>
798
799 /*
800 * This is called by the pbkbc driver if no pckbd is configured.
801 * On the i386, it is used to glue in the old, deprecated console
802 * code. On the Alpha, it does nothing.
803 */
804 int
805 pckbc_machdep_cnattach(kbctag, kbcslot)
806 pckbc_tag_t kbctag;
807 pckbc_slot_t kbcslot;
808 {
809
810 return (ENXIO);
811 }
812 #endif /* NPCKBC > 0 && NPCKBD == 0 */
813
814 void
815 cpu_startup()
816 {
817 register unsigned i;
818 int base, residual;
819 vaddr_t minaddr, maxaddr;
820 vsize_t size;
821 char pbuf[9];
822 #if defined(DEBUG)
823 extern int pmapdebug;
824 int opmapdebug = pmapdebug;
825
826 pmapdebug = 0;
827 #endif
828
829 /*
830 * Good {morning,afternoon,evening,night}.
831 */
832 printf(version);
833 identifycpu();
834 format_bytes(pbuf, sizeof(pbuf), ptoa(totalphysmem));
835 printf("total memory = %s\n", pbuf);
836 format_bytes(pbuf, sizeof(pbuf), ptoa(resvmem));
837 printf("(%s reserved for PROM, ", pbuf);
838 format_bytes(pbuf, sizeof(pbuf), ptoa(physmem));
839 printf("%s used by NetBSD)\n", pbuf);
840 if (unusedmem) {
841 format_bytes(pbuf, sizeof(pbuf), ptoa(unusedmem));
842 printf("WARNING: unused memory = %s\n", pbuf);
843 }
844 if (unknownmem) {
845 format_bytes(pbuf, sizeof(pbuf), ptoa(unknownmem));
846 printf("WARNING: %s of memory with unknown purpose\n", pbuf);
847 }
848
849 /*
850 * Allocate virtual address space for file I/O buffers.
851 * Note they are different than the array of headers, 'buf',
852 * and usually occupy more virtual memory than physical.
853 */
854 size = MAXBSIZE * nbuf;
855 if (uvm_map(kernel_map, (vaddr_t *) &buffers, round_page(size),
856 NULL, UVM_UNKNOWN_OFFSET, 0,
857 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE, UVM_INH_NONE,
858 UVM_ADV_NORMAL, 0)) != KERN_SUCCESS)
859 panic("startup: cannot allocate VM for buffers");
860 base = bufpages / nbuf;
861 residual = bufpages % nbuf;
862 for (i = 0; i < nbuf; i++) {
863 vsize_t curbufsize;
864 vaddr_t curbuf;
865 struct vm_page *pg;
866
867 /*
868 * Each buffer has MAXBSIZE bytes of VM space allocated. Of
869 * that MAXBSIZE space, we allocate and map (base+1) pages
870 * for the first "residual" buffers, and then we allocate
871 * "base" pages for the rest.
872 */
873 curbuf = (vaddr_t) buffers + (i * MAXBSIZE);
874 curbufsize = NBPG * ((i < residual) ? (base+1) : base);
875
876 while (curbufsize) {
877 pg = uvm_pagealloc(NULL, 0, NULL, 0);
878 if (pg == NULL)
879 panic("cpu_startup: not enough memory for "
880 "buffer cache");
881 pmap_kenter_pa(curbuf, VM_PAGE_TO_PHYS(pg),
882 VM_PROT_READ|VM_PROT_WRITE);
883 curbuf += PAGE_SIZE;
884 curbufsize -= PAGE_SIZE;
885 }
886 }
887 /*
888 * Allocate a submap for exec arguments. This map effectively
889 * limits the number of processes exec'ing at any time.
890 */
891 exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
892 16 * NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
893
894 /*
895 * Allocate a submap for physio
896 */
897 phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
898 VM_PHYS_SIZE, 0, FALSE, NULL);
899
900 /*
901 * No need to allocate an mbuf cluster submap. Mbuf clusters
902 * are allocated via the pool allocator, and we use K0SEG to
903 * map those pages.
904 */
905
906 #if defined(DEBUG)
907 pmapdebug = opmapdebug;
908 #endif
909 format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
910 printf("avail memory = %s\n", pbuf);
911 #if 0
912 {
913 extern u_long pmap_pages_stolen;
914
915 format_bytes(pbuf, sizeof(pbuf), pmap_pages_stolen * PAGE_SIZE);
916 printf("stolen memory for VM structures = %s\n", pbuf);
917 }
918 #endif
919 format_bytes(pbuf, sizeof(pbuf), bufpages * NBPG);
920 printf("using %ld buffers containing %s of memory\n", (long)nbuf, pbuf);
921
922 /*
923 * Set up buffers, so they can be used to read disk labels.
924 */
925 bufinit();
926
927 /*
928 * Set up the HWPCB so that it's safe to configure secondary
929 * CPUs.
930 */
931 hwrpb_primary_init();
932 }
933
934 /*
935 * Retrieve the platform name from the DSR.
936 */
937 const char *
938 alpha_dsr_sysname()
939 {
940 struct dsrdb *dsr;
941 const char *sysname;
942
943 /*
944 * DSR does not exist on early HWRPB versions.
945 */
946 if (hwrpb->rpb_version < HWRPB_DSRDB_MINVERS)
947 return (NULL);
948
949 dsr = (struct dsrdb *)(((caddr_t)hwrpb) + hwrpb->rpb_dsrdb_off);
950 sysname = (const char *)((caddr_t)dsr + (dsr->dsr_sysname_off +
951 sizeof(u_int64_t)));
952 return (sysname);
953 }
954
955 /*
956 * Lookup the system specified system variation in the provided table,
957 * returning the model string on match.
958 */
959 const char *
960 alpha_variation_name(variation, avtp)
961 u_int64_t variation;
962 const struct alpha_variation_table *avtp;
963 {
964 int i;
965
966 for (i = 0; avtp[i].avt_model != NULL; i++)
967 if (avtp[i].avt_variation == variation)
968 return (avtp[i].avt_model);
969 return (NULL);
970 }
971
972 /*
973 * Generate a default platform name based for unknown system variations.
974 */
975 const char *
976 alpha_unknown_sysname()
977 {
978 static char s[128]; /* safe size */
979
980 sprintf(s, "%s family, unknown model variation 0x%lx",
981 platform.family, hwrpb->rpb_variation & SV_ST_MASK);
982 return ((const char *)s);
983 }
984
985 void
986 identifycpu()
987 {
988 char *s;
989 int i;
990
991 /*
992 * print out CPU identification information.
993 */
994 printf("%s", cpu_model);
995 for(s = cpu_model; *s; ++s)
996 if(strncasecmp(s, "MHz", 3) == 0)
997 goto skipMHz;
998 printf(", %ldMHz", hwrpb->rpb_cc_freq / 1000000);
999 skipMHz:
1000 printf(", s/n ");
1001 for (i = 0; i < 10; i++)
1002 printf("%c", hwrpb->rpb_ssn[i]);
1003 printf("\n");
1004 printf("%ld byte page size, %d processor%s.\n",
1005 hwrpb->rpb_page_size, ncpus, ncpus == 1 ? "" : "s");
1006 #if 0
1007 /* this isn't defined for any systems that we run on? */
1008 printf("serial number 0x%lx 0x%lx\n",
1009 ((long *)hwrpb->rpb_ssn)[0], ((long *)hwrpb->rpb_ssn)[1]);
1010
1011 /* and these aren't particularly useful! */
1012 printf("variation: 0x%lx, revision 0x%lx\n",
1013 hwrpb->rpb_variation, *(long *)hwrpb->rpb_revision);
1014 #endif
1015 }
1016
1017 int waittime = -1;
1018 struct pcb dumppcb;
1019
1020 void
1021 cpu_reboot(howto, bootstr)
1022 int howto;
1023 char *bootstr;
1024 {
1025 #if defined(MULTIPROCESSOR)
1026 #if 0 /* XXX See below. */
1027 u_long cpu_id;
1028 #endif
1029 #endif
1030
1031 #if defined(MULTIPROCESSOR)
1032 /* We must be running on the primary CPU. */
1033 if (alpha_pal_whami() != hwrpb->rpb_primary_cpu_id)
1034 panic("cpu_reboot: not on primary CPU!");
1035 #endif
1036
1037 /* If system is cold, just halt. */
1038 if (cold) {
1039 howto |= RB_HALT;
1040 goto haltsys;
1041 }
1042
1043 /* If "always halt" was specified as a boot flag, obey. */
1044 if ((boothowto & RB_HALT) != 0)
1045 howto |= RB_HALT;
1046
1047 boothowto = howto;
1048 if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
1049 waittime = 0;
1050 vfs_shutdown();
1051 /*
1052 * If we've been adjusting the clock, the todr
1053 * will be out of synch; adjust it now.
1054 */
1055 resettodr();
1056 }
1057
1058 /* Disable interrupts. */
1059 splhigh();
1060
1061 /* If rebooting and a dump is requested do it. */
1062 #if 0
1063 if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
1064 #else
1065 if (howto & RB_DUMP)
1066 #endif
1067 dumpsys();
1068
1069 haltsys:
1070
1071 /* run any shutdown hooks */
1072 doshutdownhooks();
1073
1074 #if defined(MULTIPROCESSOR)
1075 #if 0 /* XXX doesn't work when called from here?! */
1076 /* Kill off any secondary CPUs. */
1077 for (cpu_id = 0; cpu_id < hwrpb->rpb_pcs_cnt; cpu_id++) {
1078 if (cpu_id == hwrpb->rpb_primary_cpu_id ||
1079 cpu_info[cpu_id].ci_softc == NULL)
1080 continue;
1081 cpu_halt_secondary(cpu_id);
1082 }
1083 #endif
1084 #endif
1085
1086 #ifdef BOOTKEY
1087 printf("hit any key to %s...\n", howto & RB_HALT ? "halt" : "reboot");
1088 cnpollc(1); /* for proper keyboard command handling */
1089 cngetc();
1090 cnpollc(0);
1091 printf("\n");
1092 #endif
1093
1094 /* Finally, powerdown/halt/reboot the system. */
1095 if ((howto & RB_POWERDOWN) == RB_POWERDOWN &&
1096 platform.powerdown != NULL) {
1097 (*platform.powerdown)();
1098 printf("WARNING: powerdown failed!\n");
1099 }
1100 printf("%s\n\n", howto & RB_HALT ? "halted." : "rebooting...");
1101 prom_halt(howto & RB_HALT);
1102 /*NOTREACHED*/
1103 }
1104
1105 /*
1106 * These variables are needed by /sbin/savecore
1107 */
1108 u_long dumpmag = 0x8fca0101; /* magic number */
1109 int dumpsize = 0; /* pages */
1110 long dumplo = 0; /* blocks */
1111
1112 /*
1113 * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
1114 */
1115 int
1116 cpu_dumpsize()
1117 {
1118 int size;
1119
1120 size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
1121 ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
1122 if (roundup(size, dbtob(1)) != dbtob(1))
1123 return -1;
1124
1125 return (1);
1126 }
1127
1128 /*
1129 * cpu_dump_mempagecnt: calculate size of RAM (in pages) to be dumped.
1130 */
1131 u_long
1132 cpu_dump_mempagecnt()
1133 {
1134 u_long i, n;
1135
1136 n = 0;
1137 for (i = 0; i < mem_cluster_cnt; i++)
1138 n += atop(mem_clusters[i].size);
1139 return (n);
1140 }
1141
1142 /*
1143 * cpu_dump: dump machine-dependent kernel core dump headers.
1144 */
1145 int
1146 cpu_dump()
1147 {
1148 int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
1149 char buf[dbtob(1)];
1150 kcore_seg_t *segp;
1151 cpu_kcore_hdr_t *cpuhdrp;
1152 phys_ram_seg_t *memsegp;
1153 int i;
1154
1155 dump = bdevsw[major(dumpdev)].d_dump;
1156
1157 bzero(buf, sizeof buf);
1158 segp = (kcore_seg_t *)buf;
1159 cpuhdrp = (cpu_kcore_hdr_t *)&buf[ALIGN(sizeof(*segp))];
1160 memsegp = (phys_ram_seg_t *)&buf[ ALIGN(sizeof(*segp)) +
1161 ALIGN(sizeof(*cpuhdrp))];
1162
1163 /*
1164 * Generate a segment header.
1165 */
1166 CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
1167 segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
1168
1169 /*
1170 * Add the machine-dependent header info.
1171 */
1172 cpuhdrp->lev1map_pa = ALPHA_K0SEG_TO_PHYS((vaddr_t)kernel_lev1map);
1173 cpuhdrp->page_size = PAGE_SIZE;
1174 cpuhdrp->nmemsegs = mem_cluster_cnt;
1175
1176 /*
1177 * Fill in the memory segment descriptors.
1178 */
1179 for (i = 0; i < mem_cluster_cnt; i++) {
1180 memsegp[i].start = mem_clusters[i].start;
1181 memsegp[i].size = mem_clusters[i].size & ~PAGE_MASK;
1182 }
1183
1184 return (dump(dumpdev, dumplo, (caddr_t)buf, dbtob(1)));
1185 }
1186
1187 /*
1188 * This is called by main to set dumplo and dumpsize.
1189 * Dumps always skip the first NBPG of disk space
1190 * in case there might be a disk label stored there.
1191 * If there is extra space, put dump at the end to
1192 * reduce the chance that swapping trashes it.
1193 */
1194 void
1195 cpu_dumpconf()
1196 {
1197 int nblks, dumpblks; /* size of dump area */
1198 int maj;
1199
1200 if (dumpdev == NODEV)
1201 goto bad;
1202 maj = major(dumpdev);
1203 if (maj < 0 || maj >= nblkdev)
1204 panic("dumpconf: bad dumpdev=0x%x", dumpdev);
1205 if (bdevsw[maj].d_psize == NULL)
1206 goto bad;
1207 nblks = (*bdevsw[maj].d_psize)(dumpdev);
1208 if (nblks <= ctod(1))
1209 goto bad;
1210
1211 dumpblks = cpu_dumpsize();
1212 if (dumpblks < 0)
1213 goto bad;
1214 dumpblks += ctod(cpu_dump_mempagecnt());
1215
1216 /* If dump won't fit (incl. room for possible label), punt. */
1217 if (dumpblks > (nblks - ctod(1)))
1218 goto bad;
1219
1220 /* Put dump at end of partition */
1221 dumplo = nblks - dumpblks;
1222
1223 /* dumpsize is in page units, and doesn't include headers. */
1224 dumpsize = cpu_dump_mempagecnt();
1225 return;
1226
1227 bad:
1228 dumpsize = 0;
1229 return;
1230 }
1231
1232 /*
1233 * Dump the kernel's image to the swap partition.
1234 */
1235 #define BYTES_PER_DUMP NBPG
1236
1237 void
1238 dumpsys()
1239 {
1240 u_long totalbytesleft, bytes, i, n, memcl;
1241 u_long maddr;
1242 int psize;
1243 daddr_t blkno;
1244 int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
1245 int error;
1246
1247 /* Save registers. */
1248 savectx(&dumppcb);
1249
1250 if (dumpdev == NODEV)
1251 return;
1252
1253 /*
1254 * For dumps during autoconfiguration,
1255 * if dump device has already configured...
1256 */
1257 if (dumpsize == 0)
1258 cpu_dumpconf();
1259 if (dumplo <= 0) {
1260 printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
1261 minor(dumpdev));
1262 return;
1263 }
1264 printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
1265 minor(dumpdev), dumplo);
1266
1267 psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
1268 printf("dump ");
1269 if (psize == -1) {
1270 printf("area unavailable\n");
1271 return;
1272 }
1273
1274 /* XXX should purge all outstanding keystrokes. */
1275
1276 if ((error = cpu_dump()) != 0)
1277 goto err;
1278
1279 totalbytesleft = ptoa(cpu_dump_mempagecnt());
1280 blkno = dumplo + cpu_dumpsize();
1281 dump = bdevsw[major(dumpdev)].d_dump;
1282 error = 0;
1283
1284 for (memcl = 0; memcl < mem_cluster_cnt; memcl++) {
1285 maddr = mem_clusters[memcl].start;
1286 bytes = mem_clusters[memcl].size & ~PAGE_MASK;
1287
1288 for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
1289
1290 /* Print out how many MBs we to go. */
1291 if ((totalbytesleft % (1024*1024)) == 0)
1292 printf("%ld ", totalbytesleft / (1024 * 1024));
1293
1294 /* Limit size for next transfer. */
1295 n = bytes - i;
1296 if (n > BYTES_PER_DUMP)
1297 n = BYTES_PER_DUMP;
1298
1299 error = (*dump)(dumpdev, blkno,
1300 (caddr_t)ALPHA_PHYS_TO_K0SEG(maddr), n);
1301 if (error)
1302 goto err;
1303 maddr += n;
1304 blkno += btodb(n); /* XXX? */
1305
1306 /* XXX should look for keystrokes, to cancel. */
1307 }
1308 }
1309
1310 err:
1311 switch (error) {
1312
1313 case ENXIO:
1314 printf("device bad\n");
1315 break;
1316
1317 case EFAULT:
1318 printf("device not ready\n");
1319 break;
1320
1321 case EINVAL:
1322 printf("area improper\n");
1323 break;
1324
1325 case EIO:
1326 printf("i/o error\n");
1327 break;
1328
1329 case EINTR:
1330 printf("aborted from console\n");
1331 break;
1332
1333 case 0:
1334 printf("succeeded\n");
1335 break;
1336
1337 default:
1338 printf("error %d\n", error);
1339 break;
1340 }
1341 printf("\n\n");
1342 delay(1000);
1343 }
1344
1345 void
1346 frametoreg(framep, regp)
1347 struct trapframe *framep;
1348 struct reg *regp;
1349 {
1350
1351 regp->r_regs[R_V0] = framep->tf_regs[FRAME_V0];
1352 regp->r_regs[R_T0] = framep->tf_regs[FRAME_T0];
1353 regp->r_regs[R_T1] = framep->tf_regs[FRAME_T1];
1354 regp->r_regs[R_T2] = framep->tf_regs[FRAME_T2];
1355 regp->r_regs[R_T3] = framep->tf_regs[FRAME_T3];
1356 regp->r_regs[R_T4] = framep->tf_regs[FRAME_T4];
1357 regp->r_regs[R_T5] = framep->tf_regs[FRAME_T5];
1358 regp->r_regs[R_T6] = framep->tf_regs[FRAME_T6];
1359 regp->r_regs[R_T7] = framep->tf_regs[FRAME_T7];
1360 regp->r_regs[R_S0] = framep->tf_regs[FRAME_S0];
1361 regp->r_regs[R_S1] = framep->tf_regs[FRAME_S1];
1362 regp->r_regs[R_S2] = framep->tf_regs[FRAME_S2];
1363 regp->r_regs[R_S3] = framep->tf_regs[FRAME_S3];
1364 regp->r_regs[R_S4] = framep->tf_regs[FRAME_S4];
1365 regp->r_regs[R_S5] = framep->tf_regs[FRAME_S5];
1366 regp->r_regs[R_S6] = framep->tf_regs[FRAME_S6];
1367 regp->r_regs[R_A0] = framep->tf_regs[FRAME_A0];
1368 regp->r_regs[R_A1] = framep->tf_regs[FRAME_A1];
1369 regp->r_regs[R_A2] = framep->tf_regs[FRAME_A2];
1370 regp->r_regs[R_A3] = framep->tf_regs[FRAME_A3];
1371 regp->r_regs[R_A4] = framep->tf_regs[FRAME_A4];
1372 regp->r_regs[R_A5] = framep->tf_regs[FRAME_A5];
1373 regp->r_regs[R_T8] = framep->tf_regs[FRAME_T8];
1374 regp->r_regs[R_T9] = framep->tf_regs[FRAME_T9];
1375 regp->r_regs[R_T10] = framep->tf_regs[FRAME_T10];
1376 regp->r_regs[R_T11] = framep->tf_regs[FRAME_T11];
1377 regp->r_regs[R_RA] = framep->tf_regs[FRAME_RA];
1378 regp->r_regs[R_T12] = framep->tf_regs[FRAME_T12];
1379 regp->r_regs[R_AT] = framep->tf_regs[FRAME_AT];
1380 regp->r_regs[R_GP] = framep->tf_regs[FRAME_GP];
1381 /* regp->r_regs[R_SP] = framep->tf_regs[FRAME_SP]; XXX */
1382 regp->r_regs[R_ZERO] = 0;
1383 }
1384
1385 void
1386 regtoframe(regp, framep)
1387 struct reg *regp;
1388 struct trapframe *framep;
1389 {
1390
1391 framep->tf_regs[FRAME_V0] = regp->r_regs[R_V0];
1392 framep->tf_regs[FRAME_T0] = regp->r_regs[R_T0];
1393 framep->tf_regs[FRAME_T1] = regp->r_regs[R_T1];
1394 framep->tf_regs[FRAME_T2] = regp->r_regs[R_T2];
1395 framep->tf_regs[FRAME_T3] = regp->r_regs[R_T3];
1396 framep->tf_regs[FRAME_T4] = regp->r_regs[R_T4];
1397 framep->tf_regs[FRAME_T5] = regp->r_regs[R_T5];
1398 framep->tf_regs[FRAME_T6] = regp->r_regs[R_T6];
1399 framep->tf_regs[FRAME_T7] = regp->r_regs[R_T7];
1400 framep->tf_regs[FRAME_S0] = regp->r_regs[R_S0];
1401 framep->tf_regs[FRAME_S1] = regp->r_regs[R_S1];
1402 framep->tf_regs[FRAME_S2] = regp->r_regs[R_S2];
1403 framep->tf_regs[FRAME_S3] = regp->r_regs[R_S3];
1404 framep->tf_regs[FRAME_S4] = regp->r_regs[R_S4];
1405 framep->tf_regs[FRAME_S5] = regp->r_regs[R_S5];
1406 framep->tf_regs[FRAME_S6] = regp->r_regs[R_S6];
1407 framep->tf_regs[FRAME_A0] = regp->r_regs[R_A0];
1408 framep->tf_regs[FRAME_A1] = regp->r_regs[R_A1];
1409 framep->tf_regs[FRAME_A2] = regp->r_regs[R_A2];
1410 framep->tf_regs[FRAME_A3] = regp->r_regs[R_A3];
1411 framep->tf_regs[FRAME_A4] = regp->r_regs[R_A4];
1412 framep->tf_regs[FRAME_A5] = regp->r_regs[R_A5];
1413 framep->tf_regs[FRAME_T8] = regp->r_regs[R_T8];
1414 framep->tf_regs[FRAME_T9] = regp->r_regs[R_T9];
1415 framep->tf_regs[FRAME_T10] = regp->r_regs[R_T10];
1416 framep->tf_regs[FRAME_T11] = regp->r_regs[R_T11];
1417 framep->tf_regs[FRAME_RA] = regp->r_regs[R_RA];
1418 framep->tf_regs[FRAME_T12] = regp->r_regs[R_T12];
1419 framep->tf_regs[FRAME_AT] = regp->r_regs[R_AT];
1420 framep->tf_regs[FRAME_GP] = regp->r_regs[R_GP];
1421 /* framep->tf_regs[FRAME_SP] = regp->r_regs[R_SP]; XXX */
1422 /* ??? = regp->r_regs[R_ZERO]; */
1423 }
1424
1425 void
1426 printregs(regp)
1427 struct reg *regp;
1428 {
1429 int i;
1430
1431 for (i = 0; i < 32; i++)
1432 printf("R%d:\t0x%016lx%s", i, regp->r_regs[i],
1433 i & 1 ? "\n" : "\t");
1434 }
1435
1436 void
1437 regdump(framep)
1438 struct trapframe *framep;
1439 {
1440 struct reg reg;
1441
1442 frametoreg(framep, ®);
1443 reg.r_regs[R_SP] = alpha_pal_rdusp();
1444
1445 printf("REGISTERS:\n");
1446 printregs(®);
1447 }
1448
1449 #ifdef DEBUG
1450 int sigdebug = 0;
1451 int sigpid = 0;
1452 #define SDB_FOLLOW 0x01
1453 #define SDB_KSTACK 0x02
1454 #endif
1455
1456 /*
1457 * Send an interrupt to process.
1458 */
1459 void
1460 sendsig(catcher, sig, mask, code)
1461 sig_t catcher;
1462 int sig;
1463 sigset_t *mask;
1464 u_long code;
1465 {
1466 struct proc *p = curproc;
1467 struct sigcontext *scp, ksc;
1468 struct trapframe *frame;
1469 struct sigacts *psp = p->p_sigacts;
1470 int onstack, fsize, rndfsize;
1471
1472 frame = p->p_md.md_tf;
1473
1474 /* Do we need to jump onto the signal stack? */
1475 onstack =
1476 (psp->ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
1477 (psp->ps_sigact[sig].sa_flags & SA_ONSTACK) != 0;
1478
1479 /* Allocate space for the signal handler context. */
1480 fsize = sizeof(ksc);
1481 rndfsize = ((fsize + 15) / 16) * 16;
1482
1483 if (onstack)
1484 scp = (struct sigcontext *)((caddr_t)psp->ps_sigstk.ss_sp +
1485 psp->ps_sigstk.ss_size);
1486 else
1487 scp = (struct sigcontext *)(alpha_pal_rdusp());
1488 scp = (struct sigcontext *)((caddr_t)scp - rndfsize);
1489
1490 #ifdef DEBUG
1491 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1492 printf("sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
1493 sig, &onstack, scp);
1494 #endif
1495
1496 /* Build stack frame for signal trampoline. */
1497 ksc.sc_pc = frame->tf_regs[FRAME_PC];
1498 ksc.sc_ps = frame->tf_regs[FRAME_PS];
1499
1500 /* Save register context. */
1501 frametoreg(frame, (struct reg *)ksc.sc_regs);
1502 ksc.sc_regs[R_ZERO] = 0xACEDBADE; /* magic number */
1503 ksc.sc_regs[R_SP] = alpha_pal_rdusp();
1504
1505 /* save the floating-point state, if necessary, then copy it. */
1506 if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1507 synchronize_fpstate(p, 1);
1508 ksc.sc_ownedfp = p->p_md.md_flags & MDP_FPUSED;
1509 bcopy(&p->p_addr->u_pcb.pcb_fp, (struct fpreg *)ksc.sc_fpregs,
1510 sizeof(struct fpreg));
1511 ksc.sc_fp_control = 0; /* XXX ? */
1512 bzero(ksc.sc_reserved, sizeof ksc.sc_reserved); /* XXX */
1513 bzero(ksc.sc_xxx, sizeof ksc.sc_xxx); /* XXX */
1514
1515 /* Save signal stack. */
1516 ksc.sc_onstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
1517
1518 /* Save signal mask. */
1519 ksc.sc_mask = *mask;
1520
1521 #ifdef COMPAT_13
1522 /*
1523 * XXX We always have to save an old style signal mask because
1524 * XXX we might be delivering a signal to a process which will
1525 * XXX escape from the signal in a non-standard way and invoke
1526 * XXX sigreturn() directly.
1527 */
1528 {
1529 /* Note: it's a long in the stack frame. */
1530 sigset13_t mask13;
1531
1532 native_sigset_to_sigset13(mask, &mask13);
1533 ksc.__sc_mask13 = mask13;
1534 }
1535 #endif
1536
1537 #ifdef COMPAT_OSF1
1538 /*
1539 * XXX Create an OSF/1-style sigcontext and associated goo.
1540 */
1541 #endif
1542
1543 if (copyout(&ksc, (caddr_t)scp, fsize) != 0) {
1544 /*
1545 * Process has trashed its stack; give it an illegal
1546 * instruction to halt it in its tracks.
1547 */
1548 #ifdef DEBUG
1549 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1550 printf("sendsig(%d): copyout failed on sig %d\n",
1551 p->p_pid, sig);
1552 #endif
1553 sigexit(p, SIGILL);
1554 /* NOTREACHED */
1555 }
1556 #ifdef DEBUG
1557 if (sigdebug & SDB_FOLLOW)
1558 printf("sendsig(%d): sig %d scp %p code %lx\n", p->p_pid, sig,
1559 scp, code);
1560 #endif
1561
1562 /* Set up the registers to return to sigcode. */
1563 frame->tf_regs[FRAME_PC] = (u_int64_t)psp->ps_sigcode;
1564 frame->tf_regs[FRAME_A0] = sig;
1565 frame->tf_regs[FRAME_A1] = code;
1566 frame->tf_regs[FRAME_A2] = (u_int64_t)scp;
1567 frame->tf_regs[FRAME_T12] = (u_int64_t)catcher; /* t12 is pv */
1568 alpha_pal_wrusp((unsigned long)scp);
1569
1570 /* Remember that we're now on the signal stack. */
1571 if (onstack)
1572 psp->ps_sigstk.ss_flags |= SS_ONSTACK;
1573
1574 #ifdef DEBUG
1575 if (sigdebug & SDB_FOLLOW)
1576 printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid,
1577 frame->tf_regs[FRAME_PC], frame->tf_regs[FRAME_A3]);
1578 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
1579 printf("sendsig(%d): sig %d returns\n",
1580 p->p_pid, sig);
1581 #endif
1582 }
1583
1584 /*
1585 * System call to cleanup state after a signal
1586 * has been taken. Reset signal mask and
1587 * stack state from context left by sendsig (above).
1588 * Return to previous pc and psl as specified by
1589 * context left by sendsig. Check carefully to
1590 * make sure that the user has not modified the
1591 * psl to gain improper privileges or to cause
1592 * a machine fault.
1593 */
1594 /* ARGSUSED */
1595 int
1596 sys___sigreturn14(p, v, retval)
1597 struct proc *p;
1598 void *v;
1599 register_t *retval;
1600 {
1601 struct sys___sigreturn14_args /* {
1602 syscallarg(struct sigcontext *) sigcntxp;
1603 } */ *uap = v;
1604 struct sigcontext *scp, ksc;
1605
1606 /*
1607 * The trampoline code hands us the context.
1608 * It is unsafe to keep track of it ourselves, in the event that a
1609 * program jumps out of a signal handler.
1610 */
1611 scp = SCARG(uap, sigcntxp);
1612 #ifdef DEBUG
1613 if (sigdebug & SDB_FOLLOW)
1614 printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp);
1615 #endif
1616 if (ALIGN(scp) != (u_int64_t)scp)
1617 return (EINVAL);
1618
1619 if (copyin((caddr_t)scp, &ksc, sizeof(ksc)) != 0)
1620 return (EFAULT);
1621
1622 if (ksc.sc_regs[R_ZERO] != 0xACEDBADE) /* magic number */
1623 return (EINVAL);
1624
1625 /* Restore register context. */
1626 p->p_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc;
1627 p->p_md.md_tf->tf_regs[FRAME_PS] =
1628 (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR;
1629
1630 regtoframe((struct reg *)ksc.sc_regs, p->p_md.md_tf);
1631 alpha_pal_wrusp(ksc.sc_regs[R_SP]);
1632
1633 /* XXX ksc.sc_ownedfp ? */
1634 if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1635 synchronize_fpstate(p, 0);
1636 bcopy((struct fpreg *)ksc.sc_fpregs, &p->p_addr->u_pcb.pcb_fp,
1637 sizeof(struct fpreg));
1638 /* XXX ksc.sc_fp_control ? */
1639
1640 /* Restore signal stack. */
1641 if (ksc.sc_onstack & SS_ONSTACK)
1642 p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
1643 else
1644 p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
1645
1646 /* Restore signal mask. */
1647 (void) sigprocmask1(p, SIG_SETMASK, &ksc.sc_mask, 0);
1648
1649 #ifdef DEBUG
1650 if (sigdebug & SDB_FOLLOW)
1651 printf("sigreturn(%d): returns\n", p->p_pid);
1652 #endif
1653 return (EJUSTRETURN);
1654 }
1655
1656 /*
1657 * machine dependent system variables.
1658 */
1659 int
1660 cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
1661 int *name;
1662 u_int namelen;
1663 void *oldp;
1664 size_t *oldlenp;
1665 void *newp;
1666 size_t newlen;
1667 struct proc *p;
1668 {
1669 dev_t consdev;
1670
1671 /* all sysctl names at this level are terminal */
1672 if (namelen != 1)
1673 return (ENOTDIR); /* overloaded */
1674
1675 switch (name[0]) {
1676 case CPU_CONSDEV:
1677 if (cn_tab != NULL)
1678 consdev = cn_tab->cn_dev;
1679 else
1680 consdev = NODEV;
1681 return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
1682 sizeof consdev));
1683
1684 case CPU_ROOT_DEVICE:
1685 return (sysctl_rdstring(oldp, oldlenp, newp,
1686 root_device->dv_xname));
1687
1688 case CPU_UNALIGNED_PRINT:
1689 return (sysctl_int(oldp, oldlenp, newp, newlen,
1690 &alpha_unaligned_print));
1691
1692 case CPU_UNALIGNED_FIX:
1693 return (sysctl_int(oldp, oldlenp, newp, newlen,
1694 &alpha_unaligned_fix));
1695
1696 case CPU_UNALIGNED_SIGBUS:
1697 return (sysctl_int(oldp, oldlenp, newp, newlen,
1698 &alpha_unaligned_sigbus));
1699
1700 case CPU_BOOTED_KERNEL:
1701 return (sysctl_rdstring(oldp, oldlenp, newp,
1702 bootinfo.booted_kernel));
1703
1704 default:
1705 return (EOPNOTSUPP);
1706 }
1707 /* NOTREACHED */
1708 }
1709
1710 /*
1711 * Set registers on exec.
1712 */
1713 void
1714 setregs(p, pack, stack)
1715 register struct proc *p;
1716 struct exec_package *pack;
1717 u_long stack;
1718 {
1719 struct trapframe *tfp = p->p_md.md_tf;
1720 #ifdef DEBUG
1721 int i;
1722 #endif
1723
1724 #ifdef DEBUG
1725 /*
1726 * Crash and dump, if the user requested it.
1727 */
1728 if (boothowto & RB_DUMP)
1729 panic("crash requested by boot flags");
1730 #endif
1731
1732 #ifdef DEBUG
1733 for (i = 0; i < FRAME_SIZE; i++)
1734 tfp->tf_regs[i] = 0xbabefacedeadbeef;
1735 #else
1736 bzero(tfp->tf_regs, FRAME_SIZE * sizeof tfp->tf_regs[0]);
1737 #endif
1738 bzero(&p->p_addr->u_pcb.pcb_fp, sizeof p->p_addr->u_pcb.pcb_fp);
1739 p->p_addr->u_pcb.pcb_fp.fpr_cr = FPCR_INED
1740 | FPCR_UNFD
1741 | FPCR_UNDZ
1742 | FPCR_DYN(FP_RN)
1743 | FPCR_OVFD
1744 | FPCR_DZED
1745 | FPCR_INVD
1746 | FPCR_DNZ;
1747 alpha_pal_wrusp(stack);
1748 tfp->tf_regs[FRAME_PS] = ALPHA_PSL_USERSET;
1749 tfp->tf_regs[FRAME_PC] = pack->ep_entry & ~3;
1750
1751 tfp->tf_regs[FRAME_A0] = stack; /* a0 = sp */
1752 tfp->tf_regs[FRAME_A1] = 0; /* a1 = rtld cleanup */
1753 tfp->tf_regs[FRAME_A2] = 0; /* a2 = rtld object */
1754 tfp->tf_regs[FRAME_A3] = (u_int64_t)PS_STRINGS; /* a3 = ps_strings */
1755 tfp->tf_regs[FRAME_T12] = tfp->tf_regs[FRAME_PC]; /* a.k.a. PV */
1756
1757 p->p_md.md_flags &= ~MDP_FPUSED;
1758 if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
1759 synchronize_fpstate(p, 0);
1760 }
1761
1762 /*
1763 * Release the FPU.
1764 */
1765 void
1766 release_fpu(int save)
1767 {
1768 struct proc *p;
1769 int s;
1770
1771 s = splhigh();
1772 if ((p = fpcurproc) == NULL) {
1773 splx(s);
1774 return;
1775 }
1776 fpcurproc = NULL;
1777 splx(s);
1778
1779 if (save) {
1780 alpha_pal_wrfen(1);
1781 savefpstate(&p->p_addr->u_pcb.pcb_fp);
1782 #if defined(MULTIPROCESSOR)
1783 alpha_mb();
1784 #endif
1785 alpha_pal_wrfen(0);
1786 }
1787
1788 p->p_addr->u_pcb.pcb_fpcpu = NULL;
1789 #if defined(MULTIPROCESSOR)
1790 alpha_mb();
1791 #endif
1792 }
1793
1794 /*
1795 * Synchronize FP state for this process.
1796 */
1797 void
1798 synchronize_fpstate(struct proc *p, int save)
1799 {
1800
1801 if (p->p_addr->u_pcb.pcb_fpcpu == NULL) {
1802 /* Already in-sync. */
1803 return;
1804 }
1805
1806 #if defined(MULTIPROCESSOR)
1807 if (p->p_addr->u_pcb.pcb_fpcpu == curcpu()) {
1808 KASSERT(fpcurproc == p);
1809 release_fpu(save);
1810 } else {
1811 alpha_send_ipi(p->p_addr->u_pcb.pcb_fpcpu->ci_cpuid, save ?
1812 ALPHA_IPI_SYNCH_FPU : ALPHA_IPI_DISCARD_FPU);
1813 do {
1814 alpha_mb();
1815 } while (p->p_addr->u_pcb.pcb_fpcpu != NULL);
1816 }
1817 #else
1818 KASSERT(fpcurproc == p);
1819 release_fpu(save);
1820 #endif /* MULTIPROCESSOR */
1821 }
1822
1823 void
1824 spl0()
1825 {
1826
1827 if (ssir) {
1828 (void) alpha_pal_swpipl(ALPHA_PSL_IPL_SOFT);
1829 softintr_dispatch();
1830 }
1831
1832 (void) alpha_pal_swpipl(ALPHA_PSL_IPL_0);
1833 }
1834
1835 /*
1836 * The following primitives manipulate the run queues. _whichqs tells which
1837 * of the 32 queues _qs have processes in them. Setrunqueue puts processes
1838 * into queues, Remrunqueue removes them from queues. The running process is
1839 * on no queue, other processes are on a queue related to p->p_priority,
1840 * divided by 4 actually to shrink the 0-127 range of priorities into the 32
1841 * available queues.
1842 */
1843 /*
1844 * setrunqueue(p)
1845 * proc *p;
1846 *
1847 * Call should be made at splclock(), and p->p_stat should be SRUN.
1848 */
1849
1850 void
1851 setrunqueue(p)
1852 struct proc *p;
1853 {
1854 int bit;
1855
1856 /* firewall: p->p_back must be NULL */
1857 if (p->p_back != NULL)
1858 panic("setrunqueue");
1859
1860 bit = p->p_priority >> 2;
1861 sched_whichqs |= (1 << bit);
1862 p->p_forw = (struct proc *)&sched_qs[bit];
1863 p->p_back = sched_qs[bit].ph_rlink;
1864 p->p_back->p_forw = p;
1865 sched_qs[bit].ph_rlink = p;
1866 }
1867
1868 /*
1869 * remrunqueue(p)
1870 *
1871 * Call should be made at splclock().
1872 */
1873 void
1874 remrunqueue(p)
1875 struct proc *p;
1876 {
1877 int bit;
1878
1879 bit = p->p_priority >> 2;
1880 if ((sched_whichqs & (1 << bit)) == 0)
1881 panic("remrunqueue");
1882
1883 p->p_back->p_forw = p->p_forw;
1884 p->p_forw->p_back = p->p_back;
1885 p->p_back = NULL; /* for firewall checking. */
1886
1887 if ((struct proc *)&sched_qs[bit] == sched_qs[bit].ph_link)
1888 sched_whichqs &= ~(1 << bit);
1889 }
1890
1891 /*
1892 * Return the best possible estimate of the time in the timeval
1893 * to which tvp points. Unfortunately, we can't read the hardware registers.
1894 * We guarantee that the time will be greater than the value obtained by a
1895 * previous call.
1896 */
1897 void
1898 microtime(tvp)
1899 register struct timeval *tvp;
1900 {
1901 int s = splclock();
1902 static struct timeval lasttime;
1903
1904 *tvp = time;
1905 #ifdef notdef
1906 tvp->tv_usec += clkread();
1907 while (tvp->tv_usec >= 1000000) {
1908 tvp->tv_sec++;
1909 tvp->tv_usec -= 1000000;
1910 }
1911 #endif
1912 if (tvp->tv_sec == lasttime.tv_sec &&
1913 tvp->tv_usec <= lasttime.tv_usec &&
1914 (tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000) {
1915 tvp->tv_sec++;
1916 tvp->tv_usec -= 1000000;
1917 }
1918 lasttime = *tvp;
1919 splx(s);
1920 }
1921
1922 /*
1923 * Wait "n" microseconds.
1924 */
1925 void
1926 delay(n)
1927 unsigned long n;
1928 {
1929 unsigned long pcc0, pcc1, curcycle, cycles, usec;
1930
1931 if (n == 0)
1932 return;
1933
1934 pcc0 = alpha_rpcc() & 0xffffffffUL;
1935 cycles = 0;
1936 usec = 0;
1937
1938 while (usec <= n) {
1939 /*
1940 * Get the next CPU cycle count- assumes that we cannot
1941 * have had more than one 32 bit overflow.
1942 */
1943 pcc1 = alpha_rpcc() & 0xffffffffUL;
1944 if (pcc1 < pcc0)
1945 curcycle = (pcc1 + 0x100000000UL) - pcc0;
1946 else
1947 curcycle = pcc1 - pcc0;
1948
1949 /*
1950 * We now have the number of processor cycles since we
1951 * last checked. Add the current cycle count to the
1952 * running total. If it's over cycles_per_usec, increment
1953 * the usec counter.
1954 */
1955 cycles += curcycle;
1956 while (cycles > cycles_per_usec) {
1957 usec++;
1958 cycles -= cycles_per_usec;
1959 }
1960 pcc0 = pcc1;
1961 }
1962 }
1963
1964 #if defined(COMPAT_OSF1) || 1 /* XXX */
1965 void cpu_exec_ecoff_setregs __P((struct proc *, struct exec_package *,
1966 u_long));
1967
1968 void
1969 cpu_exec_ecoff_setregs(p, epp, stack)
1970 struct proc *p;
1971 struct exec_package *epp;
1972 u_long stack;
1973 {
1974 struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
1975
1976 setregs(p, epp, stack);
1977 p->p_md.md_tf->tf_regs[FRAME_GP] = execp->a.gp_value;
1978 }
1979
1980 /*
1981 * cpu_exec_ecoff_hook():
1982 * cpu-dependent ECOFF format hook for execve().
1983 *
1984 * Do any machine-dependent diddling of the exec package when doing ECOFF.
1985 *
1986 */
1987 int
1988 cpu_exec_ecoff_hook(p, epp)
1989 struct proc *p;
1990 struct exec_package *epp;
1991 {
1992 struct ecoff_exechdr *execp = (struct ecoff_exechdr *)epp->ep_hdr;
1993 extern struct emul emul_netbsd;
1994 int error;
1995 extern int osf1_exec_ecoff_hook(struct proc *p,
1996 struct exec_package *epp);
1997
1998 switch (execp->f.f_magic) {
1999 #ifdef COMPAT_OSF1
2000 case ECOFF_MAGIC_ALPHA:
2001 error = osf1_exec_ecoff_hook(p, epp);
2002 break;
2003 #endif
2004
2005 case ECOFF_MAGIC_NETBSD_ALPHA:
2006 epp->ep_emul = &emul_netbsd;
2007 error = 0;
2008 break;
2009
2010 default:
2011 error = ENOEXEC;
2012 }
2013 return (error);
2014 }
2015 #endif
2016
2017 int
2018 alpha_pa_access(pa)
2019 u_long pa;
2020 {
2021 int i;
2022
2023 for (i = 0; i < mem_cluster_cnt; i++) {
2024 if (pa < mem_clusters[i].start)
2025 continue;
2026 if ((pa - mem_clusters[i].start) >=
2027 (mem_clusters[i].size & ~PAGE_MASK))
2028 continue;
2029 return (mem_clusters[i].size & PAGE_MASK); /* prot */
2030 }
2031
2032 /*
2033 * Address is not a memory address. If we're secure, disallow
2034 * access. Otherwise, grant read/write.
2035 */
2036 if (securelevel > 0)
2037 return (PROT_NONE);
2038 else
2039 return (PROT_READ | PROT_WRITE);
2040 }
2041
2042 /* XXX XXX BEGIN XXX XXX */
2043 paddr_t alpha_XXX_dmamap_or; /* XXX */
2044 /* XXX */
2045 paddr_t /* XXX */
2046 alpha_XXX_dmamap(v) /* XXX */
2047 vaddr_t v; /* XXX */
2048 { /* XXX */
2049 /* XXX */
2050 return (vtophys(v) | alpha_XXX_dmamap_or); /* XXX */
2051 } /* XXX */
2052 /* XXX XXX END XXX XXX */
2053
2054 char *
2055 dot_conv(x)
2056 unsigned long x;
2057 {
2058 int i;
2059 char *xc;
2060 static int next;
2061 static char space[2][20];
2062
2063 xc = space[next ^= 1] + sizeof space[0];
2064 *--xc = '\0';
2065 for (i = 0;; ++i) {
2066 if (i && (i & 3) == 0)
2067 *--xc = '.';
2068 *--xc = "0123456789abcdef"[x & 0xf];
2069 x >>= 4;
2070 if (x == 0)
2071 break;
2072 }
2073 return xc;
2074 }
2075