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