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