1/*
2 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3 * Copyright (C) 1995, 1996 TooLs GmbH.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31/*
32 * Copyright (C) 2001 Benno Rice
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 *
44 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
49 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
50 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
52 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
53 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 * $NetBSD: machdep.c,v 1.74.2.1 2000/11/01 16:13:48 tv Exp $
55 */
56
57#ifndef lint
58static const char rcsid[] =
59 "$FreeBSD: head/sys/powerpc/aim/machdep.c 106605 2002-11-07 23:57:17Z tmm $";
60#endif /* not lint */
61
62#include "opt_ddb.h"
63#include "opt_compat.h"
64#include "opt_msgbuf.h"
65
66#include <sys/param.h>
67#include <sys/systm.h>
68#include <sys/eventhandler.h>
69#include <sys/imgact.h>
70#include <sys/sysproto.h>
71#include <sys/lock.h>
72#include <sys/mutex.h>
73#include <sys/ktr.h>
74#include <sys/signalvar.h>
75#include <sys/kernel.h>
76#include <sys/proc.h>
77#include <sys/malloc.h>
78#include <sys/reboot.h>
79#include <sys/bio.h>
80#include <sys/buf.h>
81#include <sys/bus.h>
82#include <sys/mbuf.h>
83#include <sys/vmmeter.h>
84#include <sys/msgbuf.h>
85#include <sys/exec.h>
86#include <sys/sysctl.h>
87#include <sys/uio.h>
88#include <sys/linker.h>
89#include <sys/cons.h>
90#include <sys/ucontext.h>
91#include <sys/sysent.h>
92#include <net/netisr.h>
93#include <vm/vm.h>
94#include <vm/vm_kern.h>
95#include <vm/vm_page.h>
96#include <vm/vm_map.h>
97#include <vm/vm_extern.h>
98#include <vm/vm_object.h>
99#include <vm/vm_pager.h>
100#include <sys/user.h>
101#include <sys/ptrace.h>
102#include <machine/bat.h>
103#include <machine/clock.h>
104#include <machine/md_var.h>
105#include <machine/metadata.h>
106#include <machine/reg.h>
107#include <machine/fpu.h>
108#include <machine/vmparam.h>
109#include <machine/elf.h>
110#include <machine/trap.h>
111#include <machine/powerpc.h>
112#include <dev/ofw/openfirm.h>
113#include <ddb/ddb.h>
114#include <sys/vnode.h>
115#include <machine/sigframe.h>
116
117int cold = 1;
118
119char pcpu0[PAGE_SIZE];
120char uarea0[UAREA_PAGES * PAGE_SIZE];
121struct trapframe frame0;
122
123vm_offset_t kstack0;
124vm_offset_t kstack0_phys;
125
126char machine[] = "powerpc";
127SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
128
129static char model[128];
130SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD, model, 0, "");
131
132static int cacheline_size = CACHELINESIZE;
133SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
134 CTLFLAG_RD, &cacheline_size, 0, "");
135
136char bootpath[256];
137
138#ifdef DDB
139/* start and end of kernel symbol table */
140void *ksym_start, *ksym_end;
141#endif /* DDB */
142
143static void cpu_startup(void *);
144SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
145
146void powerpc_init(u_int, u_int, u_int, void *);
147
148int save_ofw_mapping(void);
149int restore_ofw_mapping(void);
150
151void install_extint(void (*)(void));
152
153int setfault(faultbuf); /* defined in locore.S */
154
155long Maxmem = 0;
156
157static int chosen;
158
159struct pmap ofw_pmap;
160extern int ofmsr;
161
162struct bat battable[16];
163
164static void identifycpu(void);
165
166struct kva_md_info kmi;
167
168static void
169powerpc_ofw_shutdown(void *junk, int howto)
170{
171 if (howto & RB_HALT) {
172 OF_exit();
173 }
174}
175
176static void
177cpu_startup(void *dummy)
178{
179
180 /*
181 * Good {morning,afternoon,evening,night}.
182 */
183 identifycpu();
184
185 /* startrtclock(); */
186#ifdef PERFMON
187 perfmon_init();
188#endif
189 printf("real memory = %ld (%ldK bytes)\n", ptoa(Maxmem),
190 ptoa(Maxmem) / 1024);
191
192 /*
193 * Display any holes after the first chunk of extended memory.
194 */
195 if (bootverbose) {
196 int indx;
197
198 printf("Physical memory chunk(s):\n");
199 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
200 int size1 = phys_avail[indx + 1] - phys_avail[indx];
201
202 printf("0x%08x - 0x%08x, %d bytes (%d pages)\n",
203 phys_avail[indx], phys_avail[indx + 1] - 1, size1,
204 size1 / PAGE_SIZE);
205 }
206 }
207
208 vm_ksubmap_init(&kmi);
209
210 printf("avail memory = %ld (%ldK bytes)\n", ptoa(cnt.v_free_count),
211 ptoa(cnt.v_free_count) / 1024);
212
213 /*
214 * Set up buffers, so they can be used to read disk labels.
215 */
216 bufinit();
217 vm_pager_bufferinit();
218
219 EVENTHANDLER_REGISTER(shutdown_final, powerpc_ofw_shutdown, 0,
220 SHUTDOWN_PRI_LAST);
221
222#ifdef SMP
223 /*
224 * OK, enough kmem_alloc/malloc state should be up, lets get on with it!
225 */
226 mp_start(); /* fire up the secondaries */
227 mp_announce();
228#endif /* SMP */
229}
230
231void
232identifycpu()
233{
234 unsigned int pvr, version, revision;
235
236 /*
237 * Find cpu type (Do it by OpenFirmware?)
238 */
239 __asm ("mfpvr %0" : "=r"(pvr));
240 version = pvr >> 16;
241 revision = pvr & 0xffff;
242 switch (version) {
243 case 0x0000:
244 sprintf(model, "Simulator (psim)");
245 break;
246 case 0x0001:
247 sprintf(model, "601");
248 break;
249 case 0x0003:
250 sprintf(model, "603 (Wart)");
251 break;
252 case 0x0004:
253 sprintf(model, "604 (Zephyr)");
254 break;
255 case 0x0005:
256 sprintf(model, "602 (Galahad)");
257 break;
258 case 0x0006:
259 sprintf(model, "603e (Stretch)");
260 break;
261 case 0x0007:
262 if ((revision && 0xf000) == 0x0000)
263 sprintf(model, "603ev (Valiant)");
264 else
265 sprintf(model, "603r (Goldeneye)");
266 break;
267 case 0x0008:
268 if ((revision && 0xf000) == 0x0000)
269 sprintf(model, "G3 / 750 (Arthur)");
270 else
271 sprintf(model, "G3 / 755 (Goldfinger)");
272 break;
273 case 0x0009:
274 if ((revision && 0xf000) == 0x0000)
275 sprintf(model, "604e (Sirocco)");
276 else
277 sprintf(model, "604r (Mach V)");
278 break;
279 case 0x000a:
280 sprintf(model, "604r (Mach V)");
281 break;
282 case 0x000c:
283 sprintf(model, "G4 / 7400 (Max)");
284 break;
285 case 0x0014:
286 sprintf(model, "620 (Red October)");
287 break;
288 case 0x0081:
289 sprintf(model, "8240 (Kahlua)");
290 break;
291 case 0x8000:
292 sprintf(model, "G4 / 7450 (V'ger)");
293 break;
294 case 0x800c:
295 sprintf(model, "G4 / 7410 (Nitro)");
296 break;
297 case 0x8081:
298 sprintf(model, "8245 (Kahlua II)");
299 break;
300 default:
301 sprintf(model, "Version %x", version);
302 break;
303 }
304 sprintf(model + strlen(model), " (Revision %x)", revision);
305 printf("CPU: PowerPC %s\n", model);
306}
307
308extern char kernel_text[], _end[];
309
310extern void *trapcode, *trapsize;
311extern void *alitrap, *alisize;
312extern void *dsitrap, *dsisize;
313extern void *isitrap, *isisize;
314extern void *decrint, *decrsize;
315extern void *tlbimiss, *tlbimsize;
316extern void *tlbdlmiss, *tlbdlmsize;
317extern void *tlbdsmiss, *tlbdsmsize;
318extern void *extint, *extsize;
319
320#if 0 /* XXX: interrupt handler. We'll get to this later */
321extern void ext_intr(void);
322#endif
323
324#ifdef DDB
325extern ddblow, ddbsize;
326#endif
327#ifdef IPKDB
328extern ipkdblow, ipkdbsize;
329#endif
330
331void
332powerpc_init(u_int startkernel, u_int endkernel, u_int basekernel, void *mdp)
333{
334 struct pcpu *pc;
335 vm_offset_t end, off;
336 void *kmdp;
337
338 end = 0;
339 kmdp = NULL;
340
341 /*
342 * Parse metadata if present and fetch parameters. Must be done
343 * before console is inited so cninit gets the right value of
344 * boothowto.
345 */
346 if (mdp != NULL) {
347 preload_metadata = mdp;
348 kmdp = preload_search_by_type("elf kernel");
349 if (kmdp != NULL) {
350 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
351 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
352 end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
353 }
354 }
355
356 /*
357 * Initialize the console before printing anything.
358 */
359 cninit();
360
361 /*
362 * Complain if there is no metadata.
363 */
364 if (mdp == NULL || kmdp == NULL) {
365 printf("powerpc_init: no loader metadata.\n");
366 }
367
368#ifdef DDB
369 kdb_init();
370#endif
371
372 /*
373 * XXX: Initialize the interrupt tables.
374 */
375 bcopy(&dsitrap, (void *)EXC_DSI, (size_t)&dsisize);
376 bcopy(&isitrap, (void *)EXC_ISI, (size_t)&isisize);
377 bcopy(&trapcode, (void *)EXC_EXI, (size_t)&trapsize);
378 bcopy(&trapcode, (void *)EXC_ALI, (size_t)&trapsize);
379 bcopy(&trapcode, (void *)EXC_PGM, (size_t)&trapsize);
380 bcopy(&trapcode, (void *)EXC_FPU, (size_t)&trapsize);
381 bcopy(&trapcode, (void *)EXC_DECR, (size_t)&trapsize);
382 bcopy(&trapcode, (void *)EXC_SC, (size_t)&trapsize);
383 bcopy(&trapcode, (void *)EXC_TRC, (size_t)&trapsize);
384
385 /*
386 * Start initializing proc0 and thread0.
387 */
388 proc_linkup(&proc0, &ksegrp0, &kse0, &thread0);
389 proc0.p_uarea = (struct user *)uarea0;
390 proc0.p_stats = &proc0.p_uarea->u_stats;
391 thread0.td_frame = &frame0;
392
393 /*
394 * Set up per-cpu data.
395 */
396 pc = (struct pcpu *)(pcpu0 + PAGE_SIZE) - 1;
397 pcpu_init(pc, 0, sizeof(struct pcpu));
398 pc->pc_curthread = &thread0;
399 pc->pc_curpcb = thread0.td_pcb;
400 pc->pc_cpuid = 0;
401 /* pc->pc_mid = mid; */
402
403 __asm __volatile("mtsprg 0, %0" :: "r"(pc));
404
405 mutex_init();
406
407 /*
408 * Make sure translation has been enabled
409 */
410 mtmsr(mfmsr() | PSL_IR|PSL_DR|PSL_ME|PSL_RI);
411
412 /*
413 * Initialise virtual memory.
414 */
415 pmap_bootstrap(startkernel, endkernel);
416
417 /*
418 * Initialize tunables.
419 */
420 init_param1();
421 init_param2(physmem);
422
423 /*
424 * Finish setting up thread0.
425 */
426 thread0.td_kstack = kstack0;
427 thread0.td_pcb = (struct pcb *)
428 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
429
430 /*
431 * Map and initialise the message buffer.
432 */
433 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
434 pmap_kenter((vm_offset_t)msgbufp + off, msgbuf_phys + off);
435 msgbufinit(msgbufp, MSGBUF_SIZE);
436}
437
438void
439bzero(void *buf, size_t len)
440{
441 caddr_t p;
442
443 p = buf;
444
445 while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
446 *p++ = 0;
447 len--;
448 }
449
450 while (len >= sizeof(u_long) * 8) {
451 *(u_long*) p = 0;
452 *((u_long*) p + 1) = 0;
453 *((u_long*) p + 2) = 0;
454 *((u_long*) p + 3) = 0;
455 len -= sizeof(u_long) * 8;
456 *((u_long*) p + 4) = 0;
457 *((u_long*) p + 5) = 0;
458 *((u_long*) p + 6) = 0;
459 *((u_long*) p + 7) = 0;
460 p += sizeof(u_long) * 8;
461 }
462
463 while (len >= sizeof(u_long)) {
464 *(u_long*) p = 0;
465 len -= sizeof(u_long);
466 p += sizeof(u_long);
467 }
468
469 while (len) {
470 *p++ = 0;
471 len--;
472 }
473}
474
475void
476sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
477{
478 struct trapframe *tf;
479 struct sigframe *sfp;
480 struct sigacts *psp;
481 struct sigframe sf;
482 struct thread *td;
483 struct proc *p;
484 int oonstack, rndfsize;
485
486 td = curthread;
487 p = td->td_proc;
488 psp = p->p_sigacts;
489 tf = td->td_frame;
490 oonstack = sigonstack(tf->fixreg[1]);
491
492 rndfsize = ((sizeof(sf) + 15) / 16) * 16;
493
494 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
495 catcher, sig);
496
497 /*
498 * Save user context
499 */
500 memset(&sf, 0, sizeof(sf));
501 sf.sf_uc.uc_sigmask = *mask;
502 sf.sf_uc.uc_stack = p->p_sigstk;
503 sf.sf_uc.uc_stack.ss_flags = (p->p_flag & P_ALTSTACK)
504 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
505
506 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
507 memcpy(&sf.sf_uc.uc_mcontext.mc_frame, tf, sizeof(struct trapframe));
508
509 /*
510 * Allocate and validate space for the signal handler context.
511 */
512 if ((p->p_flag & P_ALTSTACK) != 0 && !oonstack &&
513 SIGISMEMBER(psp->ps_sigonstack, sig)) {
514 sfp = (struct sigframe *)((caddr_t)p->p_sigstk.ss_sp +
515 p->p_sigstk.ss_size - rndfsize);
516 } else {
517 sfp = (struct sigframe *)(tf->fixreg[1] - rndfsize);
518 }
519 PROC_UNLOCK(p);
520
521 /*
522 * Translate the signal if appropriate (Linux emu ?)
523 */
524 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
525 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
526
527 /*
528 * Save the floating-point state, if necessary, then copy it.
529 */
530 /* XXX */
531
532 /*
533 * Set up the registers to return to sigcode.
534 *
535 * r1/sp - sigframe ptr
536 * lr - sig function, dispatched to by blrl in trampoline
537 * r3 - sig number
538 * r4 - SIGINFO ? &siginfo : exception code
539 * r5 - user context
540 * srr0 - trampoline function addr
541 */
542 tf->lr = (register_t)catcher;
543 tf->fixreg[1] = (register_t)sfp;
544 tf->fixreg[FIRSTARG] = sig;
545 tf->fixreg[FIRSTARG+2] = (register_t)&sfp->sf_uc;
546
547 PROC_LOCK(p);
548 if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) {
549 /*
550 * Signal handler installed with SA_SIGINFO.
551 */
552 tf->fixreg[FIRSTARG+1] = (register_t)&sfp->sf_si;
553
554 /*
555 * Fill siginfo structure.
556 */
557 sf.sf_si.si_signo = sig;
558 sf.sf_si.si_code = code;
559 sf.sf_si.si_addr = (void *)tf->srr0;
560 } else {
561 /* Old FreeBSD-style arguments. */
562 tf->fixreg[FIRSTARG+1] = code;
563 }
564 PROC_UNLOCK(p);
565
566 tf->srr0 = (register_t)(PS_STRINGS - *(p->p_sysent->sv_szsigcode));
567
568 /*
569 * copy the frame out to userland.
570 */
571 if (copyout((caddr_t)&sf, (caddr_t)sfp, sizeof(sf)) != 0) {
572 /*
573 * Process has trashed its stack. Kill it.
574 */
575 CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
576 PROC_LOCK(p);
577 sigexit(td, SIGILL);
578 }
579
580 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td,
581 tf->srr0, tf->fixreg[1]);
582
583 PROC_LOCK(p);
584}
585
586int
587sigreturn(struct thread *td, struct sigreturn_args *uap)
588{
589 struct trapframe *tf;
590 struct proc *p;
591 ucontext_t uc;
592
593 CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
594
595 if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
596 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
597 return (EFAULT);
598 }
599
600 /*
601 * Don't let the user set privileged MSR bits
602 */
603 tf = td->td_frame;
604 if ((uc.uc_mcontext.mc_frame.srr1 & PSL_USERSTATIC) !=
605 (tf->srr1 & PSL_USERSTATIC)) {
606 return (EINVAL);
607 }
608
609 /*
610 * Restore the user-supplied context
611 */
612 memcpy(tf, &uc.uc_mcontext.mc_frame, sizeof(struct trapframe));
613
614 p = td->td_proc;
615 PROC_LOCK(p);
616 p->p_sigmask = uc.uc_sigmask;
617 SIG_CANTMASK(p->p_sigmask);
618 signotify(p);
619 PROC_UNLOCK(p);
620
621 /*
622 * Restore FP state
623 */
624 /* XXX */
625
626 CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
627 td, tf->srr0, tf->fixreg[1]);
628
629 return (EJUSTRETURN);
630}
631
632#ifdef COMPAT_FREEBSD4
633int
634freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
635{
636
637 return sigreturn(td, (struct sigreturn_args *)uap);
638}
639#endif
640
641void
642cpu_boot(int howto)
643{
644}
645
646/*
647 * Shutdown the CPU as much as possible.
648 */
649void
650cpu_halt(void)
651{
652
653 OF_exit();
654}
655
656/*
657 * Set set up registers on exec.
658 */
659void
660exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
661{
662 struct trapframe *tf;
663 struct ps_strings arginfo;
664
665 tf = trapframe(td);
666 bzero(tf, sizeof *tf);
667 tf->fixreg[1] = -roundup(-stack + 8, 16);
668
669 /*
670 * XXX Machine-independent code has already copied arguments and
671 * XXX environment to userland. Get them back here.
672 */
673 (void)copyin((char *)PS_STRINGS, &arginfo, sizeof(arginfo));
674
675 /*
676 * Set up arguments for _start():
677 * _start(argc, argv, envp, obj, cleanup, ps_strings);
678 *
679 * Notes:
680 * - obj and cleanup are the auxilliary and termination
681 * vectors. They are fixed up by ld.elf_so.
682 * - ps_strings is a NetBSD extention, and will be
683 * ignored by executables which are strictly
684 * compliant with the SVR4 ABI.
685 *
686 * XXX We have to set both regs and retval here due to different
687 * XXX calling convention in trap.c and init_main.c.
688 */
689 /*
690 * XXX PG: these get overwritten in the syscall return code.
691 * execve() should return EJUSTRETURN, like it does on NetBSD.
692 * Emulate by setting the syscall return value cells. The
693 * registers still have to be set for init's fork trampoline.
694 */
695 td->td_retval[0] = arginfo.ps_nargvstr;
696 td->td_retval[1] = (register_t)arginfo.ps_argvstr;
697 tf->fixreg[3] = arginfo.ps_nargvstr;
698 tf->fixreg[4] = (register_t)arginfo.ps_argvstr;
699 tf->fixreg[5] = (register_t)arginfo.ps_envstr;
700 tf->fixreg[6] = 0; /* auxillary vector */
701 tf->fixreg[7] = 0; /* termination vector */
702 tf->fixreg[8] = (register_t)PS_STRINGS; /* NetBSD extension */
703
704 tf->srr0 = entry;
705 tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
706 td->td_pcb->pcb_flags = 0;
707}
708
709#if !defined(DDB)
710void
711Debugger(const char *msg)
712{
713
714 printf("Debugger(\"%s\") called.\n", msg);
715}
716#endif /* !defined(DDB) */
717
718/* XXX: dummy {fill,set}_[fp]regs */
719int
720fill_regs(struct thread *td, struct reg *regs)
721{
722
723 return (ENOSYS);
724}
725
726int
727fill_dbregs(struct thread *td, struct dbreg *dbregs)
728{
729
730 return (ENOSYS);
731}
732
733int
734fill_fpregs(struct thread *td, struct fpreg *fpregs)
735{
736
737 return (ENOSYS);
738}
739
740int
741set_regs(struct thread *td, struct reg *regs)
742{
743
744 return (ENOSYS);
745}
746
747int
748set_dbregs(struct thread *td, struct dbreg *dbregs)
749{
750
751 return (ENOSYS);
752}
753
754int
755set_fpregs(struct thread *td, struct fpreg *fpregs)
756{
757
758 return (ENOSYS);
759}
760
761int
762ptrace_set_pc(struct thread *td, unsigned long addr)
763{
764
765 /* XXX: coming soon... */
766 return (ENOSYS);
767}
768
769int
770ptrace_single_step(struct thread *td)
771{
772
773 /* XXX: coming soon... */
774 return (ENOSYS);
775}
776
777/*
778 * Initialise a struct pcpu.
779 */
780void
781cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz)
782{
783
784 pcpu->pc_current_asngen = 1;
785}
786
787/*
788 * kcopy(const void *src, void *dst, size_t len);
789 *
790 * Copy len bytes from src to dst, aborting if we encounter a fatal
791 * page fault.
792 *
793 * kcopy() _must_ save and restore the old fault handler since it is
794 * called by uiomove(), which may be in the path of servicing a non-fatal
795 * page fault.
796 */
797int
798kcopy(const void *src, void *dst, size_t len)
799{
800 struct thread *td;
801 faultbuf env, *oldfault;
802 int rv;
803
804 td = PCPU_GET(curthread);
805 oldfault = td->td_pcb->pcb_onfault;
806 if ((rv = setfault(env)) != 0) {
807 td->td_pcb->pcb_onfault = oldfault;
808 return rv;
809 }
810
811 memcpy(dst, src, len);
812
813 td->td_pcb->pcb_onfault = oldfault;
814 return (0);
815}
2 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3 * Copyright (C) 1995, 1996 TooLs GmbH.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31/*
32 * Copyright (C) 2001 Benno Rice
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 *
44 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
49 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
50 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
52 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
53 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 * $NetBSD: machdep.c,v 1.74.2.1 2000/11/01 16:13:48 tv Exp $
55 */
56
57#ifndef lint
58static const char rcsid[] =
59 "$FreeBSD: head/sys/powerpc/aim/machdep.c 106605 2002-11-07 23:57:17Z tmm $";
60#endif /* not lint */
61
62#include "opt_ddb.h"
63#include "opt_compat.h"
64#include "opt_msgbuf.h"
65
66#include <sys/param.h>
67#include <sys/systm.h>
68#include <sys/eventhandler.h>
69#include <sys/imgact.h>
70#include <sys/sysproto.h>
71#include <sys/lock.h>
72#include <sys/mutex.h>
73#include <sys/ktr.h>
74#include <sys/signalvar.h>
75#include <sys/kernel.h>
76#include <sys/proc.h>
77#include <sys/malloc.h>
78#include <sys/reboot.h>
79#include <sys/bio.h>
80#include <sys/buf.h>
81#include <sys/bus.h>
82#include <sys/mbuf.h>
83#include <sys/vmmeter.h>
84#include <sys/msgbuf.h>
85#include <sys/exec.h>
86#include <sys/sysctl.h>
87#include <sys/uio.h>
88#include <sys/linker.h>
89#include <sys/cons.h>
90#include <sys/ucontext.h>
91#include <sys/sysent.h>
92#include <net/netisr.h>
93#include <vm/vm.h>
94#include <vm/vm_kern.h>
95#include <vm/vm_page.h>
96#include <vm/vm_map.h>
97#include <vm/vm_extern.h>
98#include <vm/vm_object.h>
99#include <vm/vm_pager.h>
100#include <sys/user.h>
101#include <sys/ptrace.h>
102#include <machine/bat.h>
103#include <machine/clock.h>
104#include <machine/md_var.h>
105#include <machine/metadata.h>
106#include <machine/reg.h>
107#include <machine/fpu.h>
108#include <machine/vmparam.h>
109#include <machine/elf.h>
110#include <machine/trap.h>
111#include <machine/powerpc.h>
112#include <dev/ofw/openfirm.h>
113#include <ddb/ddb.h>
114#include <sys/vnode.h>
115#include <machine/sigframe.h>
116
117int cold = 1;
118
119char pcpu0[PAGE_SIZE];
120char uarea0[UAREA_PAGES * PAGE_SIZE];
121struct trapframe frame0;
122
123vm_offset_t kstack0;
124vm_offset_t kstack0_phys;
125
126char machine[] = "powerpc";
127SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
128
129static char model[128];
130SYSCTL_STRING(_hw, HW_MODEL, model, CTLFLAG_RD, model, 0, "");
131
132static int cacheline_size = CACHELINESIZE;
133SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
134 CTLFLAG_RD, &cacheline_size, 0, "");
135
136char bootpath[256];
137
138#ifdef DDB
139/* start and end of kernel symbol table */
140void *ksym_start, *ksym_end;
141#endif /* DDB */
142
143static void cpu_startup(void *);
144SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
145
146void powerpc_init(u_int, u_int, u_int, void *);
147
148int save_ofw_mapping(void);
149int restore_ofw_mapping(void);
150
151void install_extint(void (*)(void));
152
153int setfault(faultbuf); /* defined in locore.S */
154
155long Maxmem = 0;
156
157static int chosen;
158
159struct pmap ofw_pmap;
160extern int ofmsr;
161
162struct bat battable[16];
163
164static void identifycpu(void);
165
166struct kva_md_info kmi;
167
168static void
169powerpc_ofw_shutdown(void *junk, int howto)
170{
171 if (howto & RB_HALT) {
172 OF_exit();
173 }
174}
175
176static void
177cpu_startup(void *dummy)
178{
179
180 /*
181 * Good {morning,afternoon,evening,night}.
182 */
183 identifycpu();
184
185 /* startrtclock(); */
186#ifdef PERFMON
187 perfmon_init();
188#endif
189 printf("real memory = %ld (%ldK bytes)\n", ptoa(Maxmem),
190 ptoa(Maxmem) / 1024);
191
192 /*
193 * Display any holes after the first chunk of extended memory.
194 */
195 if (bootverbose) {
196 int indx;
197
198 printf("Physical memory chunk(s):\n");
199 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
200 int size1 = phys_avail[indx + 1] - phys_avail[indx];
201
202 printf("0x%08x - 0x%08x, %d bytes (%d pages)\n",
203 phys_avail[indx], phys_avail[indx + 1] - 1, size1,
204 size1 / PAGE_SIZE);
205 }
206 }
207
208 vm_ksubmap_init(&kmi);
209
210 printf("avail memory = %ld (%ldK bytes)\n", ptoa(cnt.v_free_count),
211 ptoa(cnt.v_free_count) / 1024);
212
213 /*
214 * Set up buffers, so they can be used to read disk labels.
215 */
216 bufinit();
217 vm_pager_bufferinit();
218
219 EVENTHANDLER_REGISTER(shutdown_final, powerpc_ofw_shutdown, 0,
220 SHUTDOWN_PRI_LAST);
221
222#ifdef SMP
223 /*
224 * OK, enough kmem_alloc/malloc state should be up, lets get on with it!
225 */
226 mp_start(); /* fire up the secondaries */
227 mp_announce();
228#endif /* SMP */
229}
230
231void
232identifycpu()
233{
234 unsigned int pvr, version, revision;
235
236 /*
237 * Find cpu type (Do it by OpenFirmware?)
238 */
239 __asm ("mfpvr %0" : "=r"(pvr));
240 version = pvr >> 16;
241 revision = pvr & 0xffff;
242 switch (version) {
243 case 0x0000:
244 sprintf(model, "Simulator (psim)");
245 break;
246 case 0x0001:
247 sprintf(model, "601");
248 break;
249 case 0x0003:
250 sprintf(model, "603 (Wart)");
251 break;
252 case 0x0004:
253 sprintf(model, "604 (Zephyr)");
254 break;
255 case 0x0005:
256 sprintf(model, "602 (Galahad)");
257 break;
258 case 0x0006:
259 sprintf(model, "603e (Stretch)");
260 break;
261 case 0x0007:
262 if ((revision && 0xf000) == 0x0000)
263 sprintf(model, "603ev (Valiant)");
264 else
265 sprintf(model, "603r (Goldeneye)");
266 break;
267 case 0x0008:
268 if ((revision && 0xf000) == 0x0000)
269 sprintf(model, "G3 / 750 (Arthur)");
270 else
271 sprintf(model, "G3 / 755 (Goldfinger)");
272 break;
273 case 0x0009:
274 if ((revision && 0xf000) == 0x0000)
275 sprintf(model, "604e (Sirocco)");
276 else
277 sprintf(model, "604r (Mach V)");
278 break;
279 case 0x000a:
280 sprintf(model, "604r (Mach V)");
281 break;
282 case 0x000c:
283 sprintf(model, "G4 / 7400 (Max)");
284 break;
285 case 0x0014:
286 sprintf(model, "620 (Red October)");
287 break;
288 case 0x0081:
289 sprintf(model, "8240 (Kahlua)");
290 break;
291 case 0x8000:
292 sprintf(model, "G4 / 7450 (V'ger)");
293 break;
294 case 0x800c:
295 sprintf(model, "G4 / 7410 (Nitro)");
296 break;
297 case 0x8081:
298 sprintf(model, "8245 (Kahlua II)");
299 break;
300 default:
301 sprintf(model, "Version %x", version);
302 break;
303 }
304 sprintf(model + strlen(model), " (Revision %x)", revision);
305 printf("CPU: PowerPC %s\n", model);
306}
307
308extern char kernel_text[], _end[];
309
310extern void *trapcode, *trapsize;
311extern void *alitrap, *alisize;
312extern void *dsitrap, *dsisize;
313extern void *isitrap, *isisize;
314extern void *decrint, *decrsize;
315extern void *tlbimiss, *tlbimsize;
316extern void *tlbdlmiss, *tlbdlmsize;
317extern void *tlbdsmiss, *tlbdsmsize;
318extern void *extint, *extsize;
319
320#if 0 /* XXX: interrupt handler. We'll get to this later */
321extern void ext_intr(void);
322#endif
323
324#ifdef DDB
325extern ddblow, ddbsize;
326#endif
327#ifdef IPKDB
328extern ipkdblow, ipkdbsize;
329#endif
330
331void
332powerpc_init(u_int startkernel, u_int endkernel, u_int basekernel, void *mdp)
333{
334 struct pcpu *pc;
335 vm_offset_t end, off;
336 void *kmdp;
337
338 end = 0;
339 kmdp = NULL;
340
341 /*
342 * Parse metadata if present and fetch parameters. Must be done
343 * before console is inited so cninit gets the right value of
344 * boothowto.
345 */
346 if (mdp != NULL) {
347 preload_metadata = mdp;
348 kmdp = preload_search_by_type("elf kernel");
349 if (kmdp != NULL) {
350 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
351 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
352 end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
353 }
354 }
355
356 /*
357 * Initialize the console before printing anything.
358 */
359 cninit();
360
361 /*
362 * Complain if there is no metadata.
363 */
364 if (mdp == NULL || kmdp == NULL) {
365 printf("powerpc_init: no loader metadata.\n");
366 }
367
368#ifdef DDB
369 kdb_init();
370#endif
371
372 /*
373 * XXX: Initialize the interrupt tables.
374 */
375 bcopy(&dsitrap, (void *)EXC_DSI, (size_t)&dsisize);
376 bcopy(&isitrap, (void *)EXC_ISI, (size_t)&isisize);
377 bcopy(&trapcode, (void *)EXC_EXI, (size_t)&trapsize);
378 bcopy(&trapcode, (void *)EXC_ALI, (size_t)&trapsize);
379 bcopy(&trapcode, (void *)EXC_PGM, (size_t)&trapsize);
380 bcopy(&trapcode, (void *)EXC_FPU, (size_t)&trapsize);
381 bcopy(&trapcode, (void *)EXC_DECR, (size_t)&trapsize);
382 bcopy(&trapcode, (void *)EXC_SC, (size_t)&trapsize);
383 bcopy(&trapcode, (void *)EXC_TRC, (size_t)&trapsize);
384
385 /*
386 * Start initializing proc0 and thread0.
387 */
388 proc_linkup(&proc0, &ksegrp0, &kse0, &thread0);
389 proc0.p_uarea = (struct user *)uarea0;
390 proc0.p_stats = &proc0.p_uarea->u_stats;
391 thread0.td_frame = &frame0;
392
393 /*
394 * Set up per-cpu data.
395 */
396 pc = (struct pcpu *)(pcpu0 + PAGE_SIZE) - 1;
397 pcpu_init(pc, 0, sizeof(struct pcpu));
398 pc->pc_curthread = &thread0;
399 pc->pc_curpcb = thread0.td_pcb;
400 pc->pc_cpuid = 0;
401 /* pc->pc_mid = mid; */
402
403 __asm __volatile("mtsprg 0, %0" :: "r"(pc));
404
405 mutex_init();
406
407 /*
408 * Make sure translation has been enabled
409 */
410 mtmsr(mfmsr() | PSL_IR|PSL_DR|PSL_ME|PSL_RI);
411
412 /*
413 * Initialise virtual memory.
414 */
415 pmap_bootstrap(startkernel, endkernel);
416
417 /*
418 * Initialize tunables.
419 */
420 init_param1();
421 init_param2(physmem);
422
423 /*
424 * Finish setting up thread0.
425 */
426 thread0.td_kstack = kstack0;
427 thread0.td_pcb = (struct pcb *)
428 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
429
430 /*
431 * Map and initialise the message buffer.
432 */
433 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
434 pmap_kenter((vm_offset_t)msgbufp + off, msgbuf_phys + off);
435 msgbufinit(msgbufp, MSGBUF_SIZE);
436}
437
438void
439bzero(void *buf, size_t len)
440{
441 caddr_t p;
442
443 p = buf;
444
445 while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
446 *p++ = 0;
447 len--;
448 }
449
450 while (len >= sizeof(u_long) * 8) {
451 *(u_long*) p = 0;
452 *((u_long*) p + 1) = 0;
453 *((u_long*) p + 2) = 0;
454 *((u_long*) p + 3) = 0;
455 len -= sizeof(u_long) * 8;
456 *((u_long*) p + 4) = 0;
457 *((u_long*) p + 5) = 0;
458 *((u_long*) p + 6) = 0;
459 *((u_long*) p + 7) = 0;
460 p += sizeof(u_long) * 8;
461 }
462
463 while (len >= sizeof(u_long)) {
464 *(u_long*) p = 0;
465 len -= sizeof(u_long);
466 p += sizeof(u_long);
467 }
468
469 while (len) {
470 *p++ = 0;
471 len--;
472 }
473}
474
475void
476sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
477{
478 struct trapframe *tf;
479 struct sigframe *sfp;
480 struct sigacts *psp;
481 struct sigframe sf;
482 struct thread *td;
483 struct proc *p;
484 int oonstack, rndfsize;
485
486 td = curthread;
487 p = td->td_proc;
488 psp = p->p_sigacts;
489 tf = td->td_frame;
490 oonstack = sigonstack(tf->fixreg[1]);
491
492 rndfsize = ((sizeof(sf) + 15) / 16) * 16;
493
494 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
495 catcher, sig);
496
497 /*
498 * Save user context
499 */
500 memset(&sf, 0, sizeof(sf));
501 sf.sf_uc.uc_sigmask = *mask;
502 sf.sf_uc.uc_stack = p->p_sigstk;
503 sf.sf_uc.uc_stack.ss_flags = (p->p_flag & P_ALTSTACK)
504 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
505
506 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
507 memcpy(&sf.sf_uc.uc_mcontext.mc_frame, tf, sizeof(struct trapframe));
508
509 /*
510 * Allocate and validate space for the signal handler context.
511 */
512 if ((p->p_flag & P_ALTSTACK) != 0 && !oonstack &&
513 SIGISMEMBER(psp->ps_sigonstack, sig)) {
514 sfp = (struct sigframe *)((caddr_t)p->p_sigstk.ss_sp +
515 p->p_sigstk.ss_size - rndfsize);
516 } else {
517 sfp = (struct sigframe *)(tf->fixreg[1] - rndfsize);
518 }
519 PROC_UNLOCK(p);
520
521 /*
522 * Translate the signal if appropriate (Linux emu ?)
523 */
524 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
525 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
526
527 /*
528 * Save the floating-point state, if necessary, then copy it.
529 */
530 /* XXX */
531
532 /*
533 * Set up the registers to return to sigcode.
534 *
535 * r1/sp - sigframe ptr
536 * lr - sig function, dispatched to by blrl in trampoline
537 * r3 - sig number
538 * r4 - SIGINFO ? &siginfo : exception code
539 * r5 - user context
540 * srr0 - trampoline function addr
541 */
542 tf->lr = (register_t)catcher;
543 tf->fixreg[1] = (register_t)sfp;
544 tf->fixreg[FIRSTARG] = sig;
545 tf->fixreg[FIRSTARG+2] = (register_t)&sfp->sf_uc;
546
547 PROC_LOCK(p);
548 if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) {
549 /*
550 * Signal handler installed with SA_SIGINFO.
551 */
552 tf->fixreg[FIRSTARG+1] = (register_t)&sfp->sf_si;
553
554 /*
555 * Fill siginfo structure.
556 */
557 sf.sf_si.si_signo = sig;
558 sf.sf_si.si_code = code;
559 sf.sf_si.si_addr = (void *)tf->srr0;
560 } else {
561 /* Old FreeBSD-style arguments. */
562 tf->fixreg[FIRSTARG+1] = code;
563 }
564 PROC_UNLOCK(p);
565
566 tf->srr0 = (register_t)(PS_STRINGS - *(p->p_sysent->sv_szsigcode));
567
568 /*
569 * copy the frame out to userland.
570 */
571 if (copyout((caddr_t)&sf, (caddr_t)sfp, sizeof(sf)) != 0) {
572 /*
573 * Process has trashed its stack. Kill it.
574 */
575 CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
576 PROC_LOCK(p);
577 sigexit(td, SIGILL);
578 }
579
580 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td,
581 tf->srr0, tf->fixreg[1]);
582
583 PROC_LOCK(p);
584}
585
586int
587sigreturn(struct thread *td, struct sigreturn_args *uap)
588{
589 struct trapframe *tf;
590 struct proc *p;
591 ucontext_t uc;
592
593 CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
594
595 if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
596 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
597 return (EFAULT);
598 }
599
600 /*
601 * Don't let the user set privileged MSR bits
602 */
603 tf = td->td_frame;
604 if ((uc.uc_mcontext.mc_frame.srr1 & PSL_USERSTATIC) !=
605 (tf->srr1 & PSL_USERSTATIC)) {
606 return (EINVAL);
607 }
608
609 /*
610 * Restore the user-supplied context
611 */
612 memcpy(tf, &uc.uc_mcontext.mc_frame, sizeof(struct trapframe));
613
614 p = td->td_proc;
615 PROC_LOCK(p);
616 p->p_sigmask = uc.uc_sigmask;
617 SIG_CANTMASK(p->p_sigmask);
618 signotify(p);
619 PROC_UNLOCK(p);
620
621 /*
622 * Restore FP state
623 */
624 /* XXX */
625
626 CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
627 td, tf->srr0, tf->fixreg[1]);
628
629 return (EJUSTRETURN);
630}
631
632#ifdef COMPAT_FREEBSD4
633int
634freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
635{
636
637 return sigreturn(td, (struct sigreturn_args *)uap);
638}
639#endif
640
641void
642cpu_boot(int howto)
643{
644}
645
646/*
647 * Shutdown the CPU as much as possible.
648 */
649void
650cpu_halt(void)
651{
652
653 OF_exit();
654}
655
656/*
657 * Set set up registers on exec.
658 */
659void
660exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
661{
662 struct trapframe *tf;
663 struct ps_strings arginfo;
664
665 tf = trapframe(td);
666 bzero(tf, sizeof *tf);
667 tf->fixreg[1] = -roundup(-stack + 8, 16);
668
669 /*
670 * XXX Machine-independent code has already copied arguments and
671 * XXX environment to userland. Get them back here.
672 */
673 (void)copyin((char *)PS_STRINGS, &arginfo, sizeof(arginfo));
674
675 /*
676 * Set up arguments for _start():
677 * _start(argc, argv, envp, obj, cleanup, ps_strings);
678 *
679 * Notes:
680 * - obj and cleanup are the auxilliary and termination
681 * vectors. They are fixed up by ld.elf_so.
682 * - ps_strings is a NetBSD extention, and will be
683 * ignored by executables which are strictly
684 * compliant with the SVR4 ABI.
685 *
686 * XXX We have to set both regs and retval here due to different
687 * XXX calling convention in trap.c and init_main.c.
688 */
689 /*
690 * XXX PG: these get overwritten in the syscall return code.
691 * execve() should return EJUSTRETURN, like it does on NetBSD.
692 * Emulate by setting the syscall return value cells. The
693 * registers still have to be set for init's fork trampoline.
694 */
695 td->td_retval[0] = arginfo.ps_nargvstr;
696 td->td_retval[1] = (register_t)arginfo.ps_argvstr;
697 tf->fixreg[3] = arginfo.ps_nargvstr;
698 tf->fixreg[4] = (register_t)arginfo.ps_argvstr;
699 tf->fixreg[5] = (register_t)arginfo.ps_envstr;
700 tf->fixreg[6] = 0; /* auxillary vector */
701 tf->fixreg[7] = 0; /* termination vector */
702 tf->fixreg[8] = (register_t)PS_STRINGS; /* NetBSD extension */
703
704 tf->srr0 = entry;
705 tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
706 td->td_pcb->pcb_flags = 0;
707}
708
709#if !defined(DDB)
710void
711Debugger(const char *msg)
712{
713
714 printf("Debugger(\"%s\") called.\n", msg);
715}
716#endif /* !defined(DDB) */
717
718/* XXX: dummy {fill,set}_[fp]regs */
719int
720fill_regs(struct thread *td, struct reg *regs)
721{
722
723 return (ENOSYS);
724}
725
726int
727fill_dbregs(struct thread *td, struct dbreg *dbregs)
728{
729
730 return (ENOSYS);
731}
732
733int
734fill_fpregs(struct thread *td, struct fpreg *fpregs)
735{
736
737 return (ENOSYS);
738}
739
740int
741set_regs(struct thread *td, struct reg *regs)
742{
743
744 return (ENOSYS);
745}
746
747int
748set_dbregs(struct thread *td, struct dbreg *dbregs)
749{
750
751 return (ENOSYS);
752}
753
754int
755set_fpregs(struct thread *td, struct fpreg *fpregs)
756{
757
758 return (ENOSYS);
759}
760
761int
762ptrace_set_pc(struct thread *td, unsigned long addr)
763{
764
765 /* XXX: coming soon... */
766 return (ENOSYS);
767}
768
769int
770ptrace_single_step(struct thread *td)
771{
772
773 /* XXX: coming soon... */
774 return (ENOSYS);
775}
776
777/*
778 * Initialise a struct pcpu.
779 */
780void
781cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz)
782{
783
784 pcpu->pc_current_asngen = 1;
785}
786
787/*
788 * kcopy(const void *src, void *dst, size_t len);
789 *
790 * Copy len bytes from src to dst, aborting if we encounter a fatal
791 * page fault.
792 *
793 * kcopy() _must_ save and restore the old fault handler since it is
794 * called by uiomove(), which may be in the path of servicing a non-fatal
795 * page fault.
796 */
797int
798kcopy(const void *src, void *dst, size_t len)
799{
800 struct thread *td;
801 faultbuf env, *oldfault;
802 int rv;
803
804 td = PCPU_GET(curthread);
805 oldfault = td->td_pcb->pcb_onfault;
806 if ((rv = setfault(env)) != 0) {
807 td->td_pcb->pcb_onfault = oldfault;
808 return rv;
809 }
810
811 memcpy(dst, src, len);
812
813 td->td_pcb->pcb_onfault = oldfault;
814 return (0);
815}