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