Suppress the error message for command -v for files with a slash.
[dragonfly.git] / sys / platform / vkernel / i386 / cpu_regs.c
CommitLineData
6f7b98e0
MD
1/*-
2 * Copyright (c) 1992 Terrence R. Lambert.
3 * Copyright (C) 1994, David Greenman
4 * Copyright (c) 1982, 1987, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * William Jolitz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91
39 * $FreeBSD: src/sys/i386/i386/machdep.c,v 1.385.2.30 2003/05/31 08:48:05 alc Exp $
7c1212ec 40 * $DragonFly: src/sys/platform/vkernel/i386/cpu_regs.c,v 1.10 2007/01/14 07:59:05 dillon Exp $
6f7b98e0
MD
41 */
42
43#include "use_ether.h"
44#include "use_npx.h"
45#include "use_isa.h"
46#include "opt_atalk.h"
47#include "opt_compat.h"
48#include "opt_ddb.h"
49#include "opt_directio.h"
50#include "opt_inet.h"
51#include "opt_ipx.h"
52#include "opt_msgbuf.h"
53#include "opt_swap.h"
54
55#include <sys/param.h>
56#include <sys/systm.h>
57#include <sys/sysproto.h>
58#include <sys/signalvar.h>
59#include <sys/kernel.h>
60#include <sys/linker.h>
61#include <sys/malloc.h>
62#include <sys/proc.h>
63#include <sys/buf.h>
64#include <sys/reboot.h>
65#include <sys/mbuf.h>
66#include <sys/msgbuf.h>
67#include <sys/sysent.h>
68#include <sys/sysctl.h>
69#include <sys/vmmeter.h>
70#include <sys/bus.h>
71#include <sys/upcall.h>
72#include <sys/usched.h>
73#include <sys/reg.h>
74
75#include <vm/vm.h>
76#include <vm/vm_param.h>
77#include <sys/lock.h>
78#include <vm/vm_kern.h>
79#include <vm/vm_object.h>
80#include <vm/vm_page.h>
81#include <vm/vm_map.h>
82#include <vm/vm_pager.h>
83#include <vm/vm_extern.h>
84
85#include <sys/thread2.h>
86
87#include <sys/user.h>
88#include <sys/exec.h>
89#include <sys/cons.h>
90
91#include <ddb/ddb.h>
92
93#include <machine/cpu.h>
94#include <machine/clock.h>
95#include <machine/specialreg.h>
96#include <machine/md_var.h>
97#include <machine/pcb_ext.h> /* pcb.h included via sys/user.h */
98#include <machine/globaldata.h> /* CPU_prvspace */
99#include <machine/smp.h>
100#ifdef PERFMON
101#include <machine/perfmon.h>
102#endif
103#include <machine/cputypes.h>
104
105#include <bus/isa/rtc.h>
106#include <machine/vm86.h>
107#include <sys/random.h>
108#include <sys/ptrace.h>
109#include <machine/sigframe.h>
110
111extern void dblfault_handler (void);
112
113#ifndef CPU_DISABLE_SSE
114static void set_fpregs_xmm (struct save87 *, struct savexmm *);
115static void fill_fpregs_xmm (struct savexmm *, struct save87 *);
116#endif /* CPU_DISABLE_SSE */
117#ifdef DIRECTIO
118extern void ffs_rawread_setup(void);
119#endif /* DIRECTIO */
120
121#ifdef SMP
122int64_t tsc_offsets[MAXCPU];
123#else
124int64_t tsc_offsets[1];
125#endif
126
127#if defined(SWTCH_OPTIM_STATS)
128extern int swtch_optim_stats;
129SYSCTL_INT(_debug, OID_AUTO, swtch_optim_stats,
130 CTLFLAG_RD, &swtch_optim_stats, 0, "");
131SYSCTL_INT(_debug, OID_AUTO, tlb_flush_count,
132 CTLFLAG_RD, &tlb_flush_count, 0, "");
133#endif
134
6f7b98e0
MD
135static int
136sysctl_hw_physmem(SYSCTL_HANDLER_ARGS)
137{
71152ac6 138 int error = sysctl_handle_int(oidp, 0, ctob((int)Maxmem), req);
6f7b98e0
MD
139 return (error);
140}
141
142SYSCTL_PROC(_hw, HW_PHYSMEM, physmem, CTLTYPE_INT|CTLFLAG_RD,
143 0, 0, sysctl_hw_physmem, "IU", "");
144
145static int
146sysctl_hw_usermem(SYSCTL_HANDLER_ARGS)
147{
148 int error = sysctl_handle_int(oidp, 0,
71152ac6 149 ctob((int)Maxmem - vmstats.v_wire_count), req);
6f7b98e0
MD
150 return (error);
151}
152
153SYSCTL_PROC(_hw, HW_USERMEM, usermem, CTLTYPE_INT|CTLFLAG_RD,
154 0, 0, sysctl_hw_usermem, "IU", "");
155
156#if 0
157
158static int
159sysctl_machdep_msgbuf(SYSCTL_HANDLER_ARGS)
160{
161 int error;
162
163 /* Unwind the buffer, so that it's linear (possibly starting with
164 * some initial nulls).
165 */
166 error=sysctl_handle_opaque(oidp,msgbufp->msg_ptr+msgbufp->msg_bufr,
167 msgbufp->msg_size-msgbufp->msg_bufr,req);
168 if(error) return(error);
169 if(msgbufp->msg_bufr>0) {
170 error=sysctl_handle_opaque(oidp,msgbufp->msg_ptr,
171 msgbufp->msg_bufr,req);
172 }
173 return(error);
174}
175
176SYSCTL_PROC(_machdep, OID_AUTO, msgbuf, CTLTYPE_STRING|CTLFLAG_RD,
177 0, 0, sysctl_machdep_msgbuf, "A","Contents of kernel message buffer");
178
179static int msgbuf_clear;
180
181static int
182sysctl_machdep_msgbuf_clear(SYSCTL_HANDLER_ARGS)
183{
184 int error;
185 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
186 req);
187 if (!error && req->newptr) {
188 /* Clear the buffer and reset write pointer */
189 bzero(msgbufp->msg_ptr,msgbufp->msg_size);
190 msgbufp->msg_bufr=msgbufp->msg_bufx=0;
191 msgbuf_clear=0;
192 }
193 return (error);
194}
195
196SYSCTL_PROC(_machdep, OID_AUTO, msgbuf_clear, CTLTYPE_INT|CTLFLAG_RW,
197 &msgbuf_clear, 0, sysctl_machdep_msgbuf_clear, "I",
198 "Clear kernel message buffer");
199
200#endif
201
202/*
203 * Send an interrupt to process.
204 *
205 * Stack is set up to allow sigcode stored
206 * at top to call routine, followed by kcall
207 * to sigreturn routine below. After sigreturn
208 * resets the signal mask, the stack, and the
209 * frame pointer, it returns to the user
210 * specified pc, psl.
211 */
212void
213sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
214{
215 struct lwp *lp = curthread->td_lwp;
216 struct proc *p = lp->lwp_proc;
217 struct trapframe *regs;
218 struct sigacts *psp = p->p_sigacts;
219 struct sigframe sf, *sfp;
220 int oonstack;
221
222 regs = lp->lwp_md.md_regs;
223 oonstack = (lp->lwp_sigstk.ss_flags & SS_ONSTACK) ? 1 : 0;
224
225 /* save user context */
226 bzero(&sf, sizeof(struct sigframe));
227 sf.sf_uc.uc_sigmask = *mask;
228 sf.sf_uc.uc_stack = lp->lwp_sigstk;
229 sf.sf_uc.uc_mcontext.mc_onstack = oonstack;
4e7c41c5 230 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_gs, sizeof(struct trapframe));
6f7b98e0 231
ce3d8c4d
MD
232 /* make the size of the saved context visible to userland */
233 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext);
234
6f7b98e0
MD
235 /* Allocate and validate space for the signal handler context. */
236 /* XXX lwp flags */
237 if ((p->p_flag & P_ALTSTACK) != 0 && !oonstack &&
238 SIGISMEMBER(psp->ps_sigonstack, sig)) {
239 sfp = (struct sigframe *)(lp->lwp_sigstk.ss_sp +
240 lp->lwp_sigstk.ss_size - sizeof(struct sigframe));
241 lp->lwp_sigstk.ss_flags |= SS_ONSTACK;
242 }
243 else
244 sfp = (struct sigframe *)regs->tf_esp - 1;
245
246 /* Translate the signal is appropriate */
247 if (p->p_sysent->sv_sigtbl) {
248 if (sig <= p->p_sysent->sv_sigsize)
249 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
250 }
251
252 /* Build the argument list for the signal handler. */
253 sf.sf_signum = sig;
254 sf.sf_ucontext = (register_t)&sfp->sf_uc;
255 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
256 /* Signal handler installed with SA_SIGINFO. */
257 sf.sf_siginfo = (register_t)&sfp->sf_si;
258 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
259
260 /* fill siginfo structure */
261 sf.sf_si.si_signo = sig;
262 sf.sf_si.si_code = code;
263 sf.sf_si.si_addr = (void*)regs->tf_err;
264 }
265 else {
266 /* Old FreeBSD-style arguments. */
267 sf.sf_siginfo = code;
268 sf.sf_addr = regs->tf_err;
269 sf.sf_ahu.sf_handler = catcher;
270 }
271
272#if 0
273 /*
274 * If we're a vm86 process, we want to save the segment registers.
275 * We also change eflags to be our emulated eflags, not the actual
276 * eflags.
277 */
278 if (regs->tf_eflags & PSL_VM) {
279 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
280 struct vm86_kernel *vm86 = &lp->lwp_thread->td_pcb->pcb_ext->ext_vm86;
281
282 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
283 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
284 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
285 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
286
287 if (vm86->vm86_has_vme == 0)
288 sf.sf_uc.uc_mcontext.mc_eflags =
289 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
290 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
291
292 /*
293 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
294 * syscalls made by the signal handler. This just avoids
295 * wasting time for our lazy fixup of such faults. PSL_NT
296 * does nothing in vm86 mode, but vm86 programs can set it
297 * almost legitimately in probes for old cpu types.
298 */
299 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
300 }
301#endif
302
303 /*
304 * Copy the sigframe out to the user's stack.
305 */
306 if (copyout(&sf, sfp, sizeof(struct sigframe)) != 0) {
307 /*
308 * Something is wrong with the stack pointer.
309 * ...Kill the process.
310 */
311 sigexit(p, SIGILL);
312 }
313
314 regs->tf_esp = (int)sfp;
315 regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
316 regs->tf_eflags &= ~PSL_T;
4e7c41c5
MD
317 regs->tf_cs = _ucodesel;
318 regs->tf_ds = _udatasel;
319 regs->tf_es = _udatasel;
320 if (regs->tf_trapno == T_PROTFLT) {
321 regs->tf_fs = _udatasel;
322 regs->tf_gs = _udatasel;
323 }
324 regs->tf_ss = _udatasel;
6f7b98e0
MD
325}
326
327/*
328 * Sanitize the trapframe for a virtual kernel passing control to a custom
329 * VM context.
330 *
331 * Allow userland to set or maintain PSL_RF, the resume flag. This flag
332 * basically controls whether the return PC should skip the first instruction
333 * (as in an explicit system call) or re-execute it (as in an exception).
334 */
335int
336cpu_sanitize_frame(struct trapframe *frame)
337{
4e7c41c5
MD
338 frame->tf_cs = _ucodesel;
339 frame->tf_ds = _udatasel;
340 frame->tf_es = _udatasel;
341#if 0
342 frame->tf_fs = _udatasel;
343 frame->tf_gs = _udatasel;
344#endif
345 frame->tf_ss = _udatasel;
346 frame->tf_eflags &= (PSL_RF | PSL_USERCHANGE);
6f7b98e0
MD
347 frame->tf_eflags |= PSL_RESERVED_DEFAULT | PSL_I;
348 return(0);
349}
350
4e7c41c5
MD
351int
352cpu_sanitize_tls(struct savetls *tls)
353{
354 struct segment_descriptor *desc;
355 int i;
356
357 for (i = 0; i < NGTLS; ++i) {
358 desc = &tls->tls[i];
359 if (desc->sd_dpl == 0 && desc->sd_type == 0)
360 continue;
361 if (desc->sd_def32 == 0)
362 return(ENXIO);
363 if (desc->sd_type != SDT_MEMRWA)
364 return(ENXIO);
365 if (desc->sd_dpl != SEL_UPL)
366 return(ENXIO);
367 if (desc->sd_xx != 0 || desc->sd_p != 1)
368 return(ENXIO);
369 }
370 return(0);
371}
372
6f7b98e0
MD
373/*
374 * sigreturn(ucontext_t *sigcntxp)
375 *
376 * System call to cleanup state after a signal
377 * has been taken. Reset signal mask and
378 * stack state from context left by sendsig (above).
379 * Return to previous pc and psl as specified by
380 * context left by sendsig. Check carefully to
381 * make sure that the user has not modified the
382 * state to gain improper privileges.
383 */
384#define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
385#define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
386
387int
388sys_sigreturn(struct sigreturn_args *uap)
389{
390 struct lwp *lp = curthread->td_lwp;
391 struct trapframe *regs;
aaf8b91f
MD
392 ucontext_t ucp;
393 int cs;
394 int eflags;
395 int error;
6f7b98e0 396
aaf8b91f
MD
397 error = copyin(uap->sigcntxp, &ucp, sizeof(ucp));
398 if (error)
399 return (error);
6f7b98e0
MD
400
401 regs = lp->lwp_md.md_regs;
aaf8b91f 402 eflags = ucp.uc_mcontext.mc_eflags;
6f7b98e0
MD
403
404#if 0
405 if (eflags & PSL_VM) {
406 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
407 struct vm86_kernel *vm86;
408
409 /*
410 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
411 * set up the vm86 area, and we can't enter vm86 mode.
412 */
413 if (lp->lwp_thread->td_pcb->pcb_ext == 0)
414 return (EINVAL);
415 vm86 = &lp->lwp_thread->td_pcb->pcb_ext->ext_vm86;
416 if (vm86->vm86_inited == 0)
417 return (EINVAL);
418
419 /* go back to user mode if both flags are set */
420 if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
421 trapsignal(lp->lwp_proc, SIGBUS, 0);
422
423 if (vm86->vm86_has_vme) {
424 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
425 (eflags & VME_USERCHANGE) | PSL_VM;
426 } else {
427 vm86->vm86_eflags = eflags; /* save VIF, VIP */
428 eflags = (tf->tf_eflags & ~VM_USERCHANGE) | (eflags & VM_USERCHANGE) | PSL_VM;
429 }
aaf8b91f 430 bcopy(&ucp.uc_mcontext.mc_gs, tf, sizeof(struct trapframe));
6f7b98e0
MD
431 tf->tf_eflags = eflags;
432 tf->tf_vm86_ds = tf->tf_ds;
433 tf->tf_vm86_es = tf->tf_es;
434 tf->tf_vm86_fs = tf->tf_fs;
4e7c41c5
MD
435 tf->tf_vm86_gs = tf->tf_gs;
436 tf->tf_ds = _udatasel;
437 tf->tf_es = _udatasel;
438#if 0
439 tf->tf_fs = _udatasel;
440 tf->tf_gs = _udatasel;
441#endif
6f7b98e0
MD
442 } else
443#endif
444 {
445 /*
446 * Don't allow users to change privileged or reserved flags.
447 */
448 /*
449 * XXX do allow users to change the privileged flag PSL_RF.
450 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
451 * should sometimes set it there too. tf_eflags is kept in
452 * the signal context during signal handling and there is no
453 * other place to remember it, so the PSL_RF bit may be
454 * corrupted by the signal handler without us knowing.
455 * Corruption of the PSL_RF bit at worst causes one more or
456 * one less debugger trap, so allowing it is fairly harmless.
457 */
458 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
459 kprintf("sigreturn: eflags = 0x%x\n", eflags);
460 return(EINVAL);
461 }
462
463 /*
464 * Don't allow users to load a valid privileged %cs. Let the
465 * hardware check for invalid selectors, excess privilege in
466 * other selectors, invalid %eip's and invalid %esp's.
467 */
aaf8b91f 468 cs = ucp.uc_mcontext.mc_cs;
6f7b98e0
MD
469 if (!CS_SECURE(cs)) {
470 kprintf("sigreturn: cs = 0x%x\n", cs);
471 trapsignal(lp->lwp_proc, SIGBUS, T_PROTFLT);
472 return(EINVAL);
473 }
aaf8b91f 474 bcopy(&ucp.uc_mcontext.mc_gs, regs, sizeof(struct trapframe));
6f7b98e0
MD
475 }
476
aaf8b91f 477 if (ucp.uc_mcontext.mc_onstack & 1)
6f7b98e0
MD
478 lp->lwp_sigstk.ss_flags |= SS_ONSTACK;
479 else
480 lp->lwp_sigstk.ss_flags &= ~SS_ONSTACK;
481
aaf8b91f 482 lp->lwp_sigmask = ucp.uc_sigmask;
6f7b98e0
MD
483 SIG_CANTMASK(lp->lwp_sigmask);
484 return(EJUSTRETURN);
485}
486
487/*
488 * Stack frame on entry to function. %eax will contain the function vector,
489 * %ecx will contain the function data. flags, ecx, and eax will have
490 * already been pushed on the stack.
491 */
492struct upc_frame {
493 register_t eax;
494 register_t ecx;
495 register_t edx;
496 register_t flags;
497 register_t oldip;
498};
499
500void
501sendupcall(struct vmupcall *vu, int morepending)
502{
503 struct lwp *lp = curthread->td_lwp;
1fdc022f 504 struct proc *p = lp->lwp_proc;
6f7b98e0
MD
505 struct trapframe *regs;
506 struct upcall upcall;
507 struct upc_frame upc_frame;
508 int crit_count = 0;
509
1fdc022f
MD
510 /*
511 * If we are a virtual kernel running an emulated user process
512 * context, switch back to the virtual kernel context before
513 * trying to post the signal.
514 */
515 if (p->p_vkernel && p->p_vkernel->vk_current) {
516 lp->lwp_md.md_regs->tf_trapno = 0;
517 vkernel_trap(p, lp->lwp_md.md_regs);
518 }
519
6f7b98e0
MD
520 /*
521 * Get the upcall data structure
522 */
523 if (copyin(lp->lwp_upcall, &upcall, sizeof(upcall)) ||
524 copyin((char *)upcall.upc_uthread + upcall.upc_critoff, &crit_count, sizeof(int))
525 ) {
526 vu->vu_pending = 0;
527 kprintf("bad upcall address\n");
528 return;
529 }
530
531 /*
532 * If the data structure is already marked pending or has a critical
533 * section count, mark the data structure as pending and return
534 * without doing an upcall. vu_pending is left set.
535 */
536 if (upcall.upc_pending || crit_count >= vu->vu_pending) {
537 if (upcall.upc_pending < vu->vu_pending) {
538 upcall.upc_pending = vu->vu_pending;
539 copyout(&upcall.upc_pending, &lp->lwp_upcall->upc_pending,
540 sizeof(upcall.upc_pending));
541 }
542 return;
543 }
544
545 /*
546 * We can run this upcall now, clear vu_pending.
547 *
548 * Bump our critical section count and set or clear the
549 * user pending flag depending on whether more upcalls are
550 * pending. The user will be responsible for calling
551 * upc_dispatch(-1) to process remaining upcalls.
552 */
553 vu->vu_pending = 0;
554 upcall.upc_pending = morepending;
555 crit_count += TDPRI_CRIT;
556 copyout(&upcall.upc_pending, &lp->lwp_upcall->upc_pending,
557 sizeof(upcall.upc_pending));
558 copyout(&crit_count, (char *)upcall.upc_uthread + upcall.upc_critoff,
559 sizeof(int));
560
561 /*
562 * Construct a stack frame and issue the upcall
563 */
564 regs = lp->lwp_md.md_regs;
565 upc_frame.eax = regs->tf_eax;
566 upc_frame.ecx = regs->tf_ecx;
567 upc_frame.edx = regs->tf_edx;
568 upc_frame.flags = regs->tf_eflags;
569 upc_frame.oldip = regs->tf_eip;
570 if (copyout(&upc_frame, (void *)(regs->tf_esp - sizeof(upc_frame)),
571 sizeof(upc_frame)) != 0) {
572 kprintf("bad stack on upcall\n");
573 } else {
574 regs->tf_eax = (register_t)vu->vu_func;
575 regs->tf_ecx = (register_t)vu->vu_data;
576 regs->tf_edx = (register_t)lp->lwp_upcall;
577 regs->tf_eip = (register_t)vu->vu_ctx;
578 regs->tf_esp -= sizeof(upc_frame);
579 }
580}
581
582/*
583 * fetchupcall occurs in the context of a system call, which means that
584 * we have to return EJUSTRETURN in order to prevent eax and edx from
585 * being overwritten by the syscall return value.
586 *
587 * if vu is not NULL we return the new context in %edx, the new data in %ecx,
588 * and the function pointer in %eax.
589 */
590int
591fetchupcall (struct vmupcall *vu, int morepending, void *rsp)
592{
593 struct upc_frame upc_frame;
594 struct lwp *lp = curthread->td_lwp;
595 struct trapframe *regs;
596 int error;
597 struct upcall upcall;
598 int crit_count;
599
600 regs = lp->lwp_md.md_regs;
601
602 error = copyout(&morepending, &lp->lwp_upcall->upc_pending, sizeof(int));
603 if (error == 0) {
604 if (vu) {
605 /*
606 * This jumps us to the next ready context.
607 */
608 vu->vu_pending = 0;
609 error = copyin(lp->lwp_upcall, &upcall, sizeof(upcall));
610 crit_count = 0;
611 if (error == 0)
612 error = copyin((char *)upcall.upc_uthread + upcall.upc_critoff, &crit_count, sizeof(int));
613 crit_count += TDPRI_CRIT;
614 if (error == 0)
615 error = copyout(&crit_count, (char *)upcall.upc_uthread + upcall.upc_critoff, sizeof(int));
616 regs->tf_eax = (register_t)vu->vu_func;
617 regs->tf_ecx = (register_t)vu->vu_data;
618 regs->tf_edx = (register_t)lp->lwp_upcall;
619 regs->tf_eip = (register_t)vu->vu_ctx;
620 regs->tf_esp = (register_t)rsp;
621 } else {
622 /*
623 * This returns us to the originally interrupted code.
624 */
625 error = copyin(rsp, &upc_frame, sizeof(upc_frame));
626 regs->tf_eax = upc_frame.eax;
627 regs->tf_ecx = upc_frame.ecx;
628 regs->tf_edx = upc_frame.edx;
629 regs->tf_eflags = (regs->tf_eflags & ~PSL_USERCHANGE) |
630 (upc_frame.flags & PSL_USERCHANGE);
631 regs->tf_eip = upc_frame.oldip;
632 regs->tf_esp = (register_t)((char *)rsp + sizeof(upc_frame));
633 }
634 }
635 if (error == 0)
636 error = EJUSTRETURN;
637 return(error);
638}
639
640/*
641 * cpu_idle() represents the idle LWKT. You cannot return from this function
642 * (unless you want to blow things up!). Instead we look for runnable threads
643 * and loop or halt as appropriate. Giant is not held on entry to the thread.
644 *
645 * The main loop is entered with a critical section held, we must release
646 * the critical section before doing anything else. lwkt_switch() will
647 * check for pending interrupts due to entering and exiting its own
648 * critical section.
649 *
650 * Note on cpu_idle_hlt: On an SMP system we rely on a scheduler IPI
651 * to wake a HLTed cpu up. However, there are cases where the idlethread
652 * will be entered with the possibility that no IPI will occur and in such
653 * cases lwkt_switch() sets TDF_IDLE_NOHLT.
654 */
655static int cpu_idle_hlt = 1;
656static int cpu_idle_hltcnt;
657static int cpu_idle_spincnt;
658SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_hlt, CTLFLAG_RW,
659 &cpu_idle_hlt, 0, "Idle loop HLT enable");
660SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_hltcnt, CTLFLAG_RW,
661 &cpu_idle_hltcnt, 0, "Idle loop entry halts");
662SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_spincnt, CTLFLAG_RW,
663 &cpu_idle_spincnt, 0, "Idle loop entry spins");
664
6f7b98e0
MD
665void
666cpu_idle(void)
667{
668 struct thread *td = curthread;
669
670 crit_exit();
671 KKASSERT(td->td_pri < TDPRI_CRIT);
672 for (;;) {
673 /*
674 * See if there are any LWKTs ready to go.
675 */
676 lwkt_switch();
677
678 /*
679 * If we are going to halt call splz unconditionally after
680 * CLIing to catch any interrupt races. Note that we are
681 * at SPL0 and interrupts are enabled.
7c1212ec
MD
682 *
683 * We must poll our mailbox signals prior to calling
684 * sigpause() in order to properly interlock with them.
6f7b98e0
MD
685 */
686 if (cpu_idle_hlt && !lwkt_runnable() &&
687 (td->td_flags & TDF_IDLE_NOHLT) == 0) {
6f7b98e0 688 splz();
7c1212ec 689 signalmailbox(NULL);
4e7c41c5
MD
690 if (!lwkt_runnable()) {
691 sigpause(0);
4e7c41c5 692 }
6f7b98e0 693#ifdef SMP
4e7c41c5 694 else {
6f7b98e0 695 __asm __volatile("pause");
4e7c41c5 696 }
6f7b98e0
MD
697#endif
698 ++cpu_idle_hltcnt;
699 } else {
700 td->td_flags &= ~TDF_IDLE_NOHLT;
701 splz();
7c1212ec 702 signalmailbox(NULL);
6f7b98e0
MD
703#ifdef SMP
704 /*__asm __volatile("sti; pause");*/
705 __asm __volatile("pause");
706#else
707 /*__asm __volatile("sti");*/
708#endif
709 ++cpu_idle_spincnt;
710 }
711 }
712}
713
714/*
715 * Clear registers on exec
716 */
717void
718setregs(struct lwp *lp, u_long entry, u_long stack, u_long ps_strings)
719{
720 struct trapframe *regs = lp->lwp_md.md_regs;
721 struct pcb *pcb = lp->lwp_thread->td_pcb;
722
6f7b98e0
MD
723 /* was i386_user_cleanup() in NetBSD */
724 user_ldt_free(pcb);
725
726 bzero((char *)regs, sizeof(struct trapframe));
727 regs->tf_eip = entry;
728 regs->tf_esp = stack;
729 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
730 regs->tf_ss = 0;
731 regs->tf_ds = 0;
732 regs->tf_es = 0;
733 regs->tf_fs = 0;
4e7c41c5 734 regs->tf_gs = 0;
6f7b98e0
MD
735 regs->tf_cs = 0;
736
737 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
738 regs->tf_ebx = ps_strings;
739
740 /*
741 * Reset the hardware debug registers if they were in use.
742 * They won't have any meaning for the newly exec'd process.
743 */
744 if (pcb->pcb_flags & PCB_DBREGS) {
745 pcb->pcb_dr0 = 0;
746 pcb->pcb_dr1 = 0;
747 pcb->pcb_dr2 = 0;
748 pcb->pcb_dr3 = 0;
749 pcb->pcb_dr6 = 0;
750 pcb->pcb_dr7 = 0;
751 if (pcb == curthread->td_pcb) {
752 /*
753 * Clear the debug registers on the running
754 * CPU, otherwise they will end up affecting
755 * the next process we switch to.
756 */
757 reset_dbregs();
758 }
759 pcb->pcb_flags &= ~PCB_DBREGS;
760 }
761
762 /*
763 * Initialize the math emulator (if any) for the current process.
764 * Actually, just clear the bit that says that the emulator has
765 * been initialized. Initialization is delayed until the process
766 * traps to the emulator (if it is done at all) mainly because
767 * emulators don't provide an entry point for initialization.
768 */
769 lp->lwp_thread->td_pcb->pcb_flags &= ~FP_SOFTFP;
770
771 /*
772 * note: do not set CR0_TS here. npxinit() must do it after clearing
773 * gd_npxthread. Otherwise a preemptive interrupt thread may panic
774 * in npxdna().
775 */
776 crit_enter();
777#if 0
778 load_cr0(rcr0() | CR0_MP);
779#endif
780
781#if NNPX > 0
782 /* Initialize the npx (if any) for the current process. */
783 npxinit(__INITIAL_NPXCW__);
784#endif
785 crit_exit();
786
787 /*
788 * note: linux emulator needs edx to be 0x0 on entry, which is
789 * handled in execve simply by setting the 64 bit syscall
790 * return value to 0.
791 */
792}
793
794void
795cpu_setregs(void)
796{
797#if 0
798 unsigned int cr0;
799
800 cr0 = rcr0();
801 cr0 |= CR0_NE; /* Done by npxinit() */
802 cr0 |= CR0_MP | CR0_TS; /* Done at every execve() too. */
803#ifdef I386_CPU
804 if (cpu_class != CPUCLASS_386)
805#endif
806 cr0 |= CR0_WP | CR0_AM;
807 load_cr0(cr0);
808 load_gs(_udatasel);
809#endif
810}
811
812static int
813sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
814{
815 int error;
816 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
817 req);
818 if (!error && req->newptr)
819 resettodr();
820 return (error);
821}
822
823SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
824 &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
825
826extern u_long bootdev; /* not a cdev_t - encoding is different */
827SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev,
828 CTLFLAG_RD, &bootdev, 0, "Boot device (not in cdev_t format)");
829
830/*
831 * Initialize 386 and configure to run kernel
832 */
833
834/*
835 * Initialize segments & interrupt table
836 */
837
838extern struct user *proc0paddr;
839
840#if 0
841
842extern inthand_t
843 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
844 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
845 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
846 IDTVEC(page), IDTVEC(mchk), IDTVEC(fpu), IDTVEC(align),
847 IDTVEC(xmm), IDTVEC(syscall),
848 IDTVEC(rsvd0);
849extern inthand_t
850 IDTVEC(int0x80_syscall);
851
852#endif
853
854#ifdef DEBUG_INTERRUPTS
855extern inthand_t *Xrsvdary[256];
856#endif
857
858int
859ptrace_set_pc(struct proc *p, unsigned long addr)
860{
861 p->p_md.md_regs->tf_eip = addr;
862 return (0);
863}
864
865int
866ptrace_single_step(struct lwp *lp)
867{
868 lp->lwp_md.md_regs->tf_eflags |= PSL_T;
869 return (0);
870}
871
872int
873fill_regs(struct lwp *lp, struct reg *regs)
874{
6f7b98e0
MD
875 struct trapframe *tp;
876
877 tp = lp->lwp_md.md_regs;
4e7c41c5 878 regs->r_gs = tp->tf_gs;
6f7b98e0
MD
879 regs->r_fs = tp->tf_fs;
880 regs->r_es = tp->tf_es;
881 regs->r_ds = tp->tf_ds;
882 regs->r_edi = tp->tf_edi;
883 regs->r_esi = tp->tf_esi;
884 regs->r_ebp = tp->tf_ebp;
885 regs->r_ebx = tp->tf_ebx;
886 regs->r_edx = tp->tf_edx;
887 regs->r_ecx = tp->tf_ecx;
888 regs->r_eax = tp->tf_eax;
889 regs->r_eip = tp->tf_eip;
890 regs->r_cs = tp->tf_cs;
891 regs->r_eflags = tp->tf_eflags;
892 regs->r_esp = tp->tf_esp;
893 regs->r_ss = tp->tf_ss;
6f7b98e0
MD
894 return (0);
895}
896
897int
898set_regs(struct lwp *lp, struct reg *regs)
899{
6f7b98e0
MD
900 struct trapframe *tp;
901
902 tp = lp->lwp_md.md_regs;
903 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
904 !CS_SECURE(regs->r_cs))
905 return (EINVAL);
4e7c41c5 906 tp->tf_gs = regs->r_gs;
6f7b98e0
MD
907 tp->tf_fs = regs->r_fs;
908 tp->tf_es = regs->r_es;
909 tp->tf_ds = regs->r_ds;
910 tp->tf_edi = regs->r_edi;
911 tp->tf_esi = regs->r_esi;
912 tp->tf_ebp = regs->r_ebp;
913 tp->tf_ebx = regs->r_ebx;
914 tp->tf_edx = regs->r_edx;
915 tp->tf_ecx = regs->r_ecx;
916 tp->tf_eax = regs->r_eax;
917 tp->tf_eip = regs->r_eip;
918 tp->tf_cs = regs->r_cs;
919 tp->tf_eflags = regs->r_eflags;
920 tp->tf_esp = regs->r_esp;
921 tp->tf_ss = regs->r_ss;
6f7b98e0
MD
922 return (0);
923}
924
925#ifndef CPU_DISABLE_SSE
926static void
927fill_fpregs_xmm(struct savexmm *sv_xmm, struct save87 *sv_87)
928{
929 struct env87 *penv_87 = &sv_87->sv_env;
930 struct envxmm *penv_xmm = &sv_xmm->sv_env;
931 int i;
932
933 /* FPU control/status */
934 penv_87->en_cw = penv_xmm->en_cw;
935 penv_87->en_sw = penv_xmm->en_sw;
936 penv_87->en_tw = penv_xmm->en_tw;
937 penv_87->en_fip = penv_xmm->en_fip;
938 penv_87->en_fcs = penv_xmm->en_fcs;
939 penv_87->en_opcode = penv_xmm->en_opcode;
940 penv_87->en_foo = penv_xmm->en_foo;
941 penv_87->en_fos = penv_xmm->en_fos;
942
943 /* FPU registers */
944 for (i = 0; i < 8; ++i)
945 sv_87->sv_ac[i] = sv_xmm->sv_fp[i].fp_acc;
946
947 sv_87->sv_ex_sw = sv_xmm->sv_ex_sw;
948}
949
950static void
951set_fpregs_xmm(struct save87 *sv_87, struct savexmm *sv_xmm)
952{
953 struct env87 *penv_87 = &sv_87->sv_env;
954 struct envxmm *penv_xmm = &sv_xmm->sv_env;
955 int i;
956
957 /* FPU control/status */
958 penv_xmm->en_cw = penv_87->en_cw;
959 penv_xmm->en_sw = penv_87->en_sw;
960 penv_xmm->en_tw = penv_87->en_tw;
961 penv_xmm->en_fip = penv_87->en_fip;
962 penv_xmm->en_fcs = penv_87->en_fcs;
963 penv_xmm->en_opcode = penv_87->en_opcode;
964 penv_xmm->en_foo = penv_87->en_foo;
965 penv_xmm->en_fos = penv_87->en_fos;
966
967 /* FPU registers */
968 for (i = 0; i < 8; ++i)
969 sv_xmm->sv_fp[i].fp_acc = sv_87->sv_ac[i];
970
971 sv_xmm->sv_ex_sw = sv_87->sv_ex_sw;
972}
973#endif /* CPU_DISABLE_SSE */
974
975int
976fill_fpregs(struct lwp *lp, struct fpreg *fpregs)
977{
978#ifndef CPU_DISABLE_SSE
979 if (cpu_fxsr) {
980 fill_fpregs_xmm(&lp->lwp_thread->td_pcb->pcb_save.sv_xmm,
981 (struct save87 *)fpregs);
982 return (0);
983 }
984#endif /* CPU_DISABLE_SSE */
985 bcopy(&lp->lwp_thread->td_pcb->pcb_save.sv_87, fpregs, sizeof *fpregs);
986 return (0);
987}
988
989int
990set_fpregs(struct lwp *lp, struct fpreg *fpregs)
991{
992#ifndef CPU_DISABLE_SSE
993 if (cpu_fxsr) {
994 set_fpregs_xmm((struct save87 *)fpregs,
995 &lp->lwp_thread->td_pcb->pcb_save.sv_xmm);
996 return (0);
997 }
998#endif /* CPU_DISABLE_SSE */
999 bcopy(fpregs, &lp->lwp_thread->td_pcb->pcb_save.sv_87, sizeof *fpregs);
1000 return (0);
1001}
1002
1003int
1004fill_dbregs(struct lwp *lp, struct dbreg *dbregs)
1005{
1006 if (lp == NULL) {
1007 dbregs->dr0 = rdr0();
1008 dbregs->dr1 = rdr1();
1009 dbregs->dr2 = rdr2();
1010 dbregs->dr3 = rdr3();
1011 dbregs->dr4 = rdr4();
1012 dbregs->dr5 = rdr5();
1013 dbregs->dr6 = rdr6();
1014 dbregs->dr7 = rdr7();
1015 } else {
1016 struct pcb *pcb;
1017
1018 pcb = lp->lwp_thread->td_pcb;
1019 dbregs->dr0 = pcb->pcb_dr0;
1020 dbregs->dr1 = pcb->pcb_dr1;
1021 dbregs->dr2 = pcb->pcb_dr2;
1022 dbregs->dr3 = pcb->pcb_dr3;
1023 dbregs->dr4 = 0;
1024 dbregs->dr5 = 0;
1025 dbregs->dr6 = pcb->pcb_dr6;
1026 dbregs->dr7 = pcb->pcb_dr7;
1027 }
1028 return (0);
1029}
1030
1031int
1032set_dbregs(struct lwp *lp, struct dbreg *dbregs)
1033{
1034 if (lp == NULL) {
1035 load_dr0(dbregs->dr0);
1036 load_dr1(dbregs->dr1);
1037 load_dr2(dbregs->dr2);
1038 load_dr3(dbregs->dr3);
1039 load_dr4(dbregs->dr4);
1040 load_dr5(dbregs->dr5);
1041 load_dr6(dbregs->dr6);
1042 load_dr7(dbregs->dr7);
1043 } else {
1044 struct pcb *pcb;
1045 struct ucred *ucred;
1046 int i;
1047 uint32_t mask1, mask2;
1048
1049 /*
1050 * Don't let an illegal value for dr7 get set. Specifically,
1051 * check for undefined settings. Setting these bit patterns
1052 * result in undefined behaviour and can lead to an unexpected
1053 * TRCTRAP.
1054 */
1055 for (i = 0, mask1 = 0x3<<16, mask2 = 0x2<<16; i < 8;
1056 i++, mask1 <<= 2, mask2 <<= 2)
1057 if ((dbregs->dr7 & mask1) == mask2)
1058 return (EINVAL);
1059
1060 pcb = lp->lwp_thread->td_pcb;
1061 ucred = lp->lwp_proc->p_ucred;
1062
1063 /*
1064 * Don't let a process set a breakpoint that is not within the
1065 * process's address space. If a process could do this, it
1066 * could halt the system by setting a breakpoint in the kernel
1067 * (if ddb was enabled). Thus, we need to check to make sure
1068 * that no breakpoints are being enabled for addresses outside
1069 * process's address space, unless, perhaps, we were called by
1070 * uid 0.
1071 *
1072 * XXX - what about when the watched area of the user's
1073 * address space is written into from within the kernel
1074 * ... wouldn't that still cause a breakpoint to be generated
1075 * from within kernel mode?
1076 */
1077
1078 if (suser_cred(ucred, 0) != 0) {
1079 if (dbregs->dr7 & 0x3) {
1080 /* dr0 is enabled */
1081 if (dbregs->dr0 >= VM_MAX_USER_ADDRESS)
1082 return (EINVAL);
1083 }
1084
1085 if (dbregs->dr7 & (0x3<<2)) {
1086 /* dr1 is enabled */
1087 if (dbregs->dr1 >= VM_MAX_USER_ADDRESS)
1088 return (EINVAL);
1089 }
1090
1091 if (dbregs->dr7 & (0x3<<4)) {
1092 /* dr2 is enabled */
1093 if (dbregs->dr2 >= VM_MAX_USER_ADDRESS)
1094 return (EINVAL);
1095 }
1096
1097 if (dbregs->dr7 & (0x3<<6)) {
1098 /* dr3 is enabled */
1099 if (dbregs->dr3 >= VM_MAX_USER_ADDRESS)
1100 return (EINVAL);
1101 }
1102 }
1103
1104 pcb->pcb_dr0 = dbregs->dr0;
1105 pcb->pcb_dr1 = dbregs->dr1;
1106 pcb->pcb_dr2 = dbregs->dr2;
1107 pcb->pcb_dr3 = dbregs->dr3;
1108 pcb->pcb_dr6 = dbregs->dr6;
1109 pcb->pcb_dr7 = dbregs->dr7;
1110
1111 pcb->pcb_flags |= PCB_DBREGS;
1112 }
1113
1114 return (0);
1115}
1116
1117#if 0
1118/*
1119 * Return > 0 if a hardware breakpoint has been hit, and the
1120 * breakpoint was in user space. Return 0, otherwise.
1121 */
1122int
1123user_dbreg_trap(void)
1124{
1125 u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */
1126 u_int32_t bp; /* breakpoint bits extracted from dr6 */
1127 int nbp; /* number of breakpoints that triggered */
1128 caddr_t addr[4]; /* breakpoint addresses */
1129 int i;
1130
1131 dr7 = rdr7();
1132 if ((dr7 & 0x000000ff) == 0) {
1133 /*
1134 * all GE and LE bits in the dr7 register are zero,
1135 * thus the trap couldn't have been caused by the
1136 * hardware debug registers
1137 */
1138 return 0;
1139 }
1140
1141 nbp = 0;
1142 dr6 = rdr6();
1143 bp = dr6 & 0x0000000f;
1144
1145 if (!bp) {
1146 /*
1147 * None of the breakpoint bits are set meaning this
1148 * trap was not caused by any of the debug registers
1149 */
1150 return 0;
1151 }
1152
1153 /*
1154 * at least one of the breakpoints were hit, check to see
1155 * which ones and if any of them are user space addresses
1156 */
1157
1158 if (bp & 0x01) {
1159 addr[nbp++] = (caddr_t)rdr0();
1160 }
1161 if (bp & 0x02) {
1162 addr[nbp++] = (caddr_t)rdr1();
1163 }
1164 if (bp & 0x04) {
1165 addr[nbp++] = (caddr_t)rdr2();
1166 }
1167 if (bp & 0x08) {
1168 addr[nbp++] = (caddr_t)rdr3();
1169 }
1170
1171 for (i=0; i<nbp; i++) {
1172 if (addr[i] <
1173 (caddr_t)VM_MAX_USER_ADDRESS) {
1174 /*
1175 * addr[i] is in user space
1176 */
1177 return nbp;
1178 }
1179 }
1180
1181 /*
1182 * None of the breakpoints are in user space.
1183 */
1184 return 0;
1185}
1186
1187#endif
1188
1189
1190#ifndef DDB
1191void
1192Debugger(const char *msg)
1193{
1194 kprintf("Debugger(\"%s\") called.\n", msg);
1195}
1196#endif /* no DDB */
1197
1198#include <sys/disklabel.h>
1199
1200/*
1201 * Determine the size of the transfer, and make sure it is
1202 * within the boundaries of the partition. Adjust transfer
1203 * if needed, and signal errors or early completion.
1204 *
1205 * On success a new bio layer is pushed with the translated
1206 * block number, and returned.
1207 */
1208struct bio *
1209bounds_check_with_label(cdev_t dev, struct bio *bio,
1210 struct disklabel *lp, int wlabel)
1211{
1212 struct bio *nbio;
1213 struct buf *bp = bio->bio_buf;
1214 struct partition *p = lp->d_partitions + dkpart(dev);
1215 int labelsect = lp->d_partitions[0].p_offset;
1216 int maxsz = p->p_size,
1217 sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
1218 daddr_t blkno = (daddr_t)(bio->bio_offset >> DEV_BSHIFT);
1219
1220 /* overwriting disk label ? */
1221 /* XXX should also protect bootstrap in first 8K */
1222 if (blkno + p->p_offset <= LABELSECTOR + labelsect &&
1223#if LABELSECTOR != 0
1224 blkno + p->p_offset + sz > LABELSECTOR + labelsect &&
1225#endif
1226 bp->b_cmd != BUF_CMD_READ && wlabel == 0) {
1227 bp->b_error = EROFS;
1228 goto error;
1229 }
1230
1231#if defined(DOSBBSECTOR) && defined(notyet)
1232 /* overwriting master boot record? */
1233 if (blkno + p->p_offset <= DOSBBSECTOR &&
1234 bp->b_cmd != BUF_CMD_READ && wlabel == 0) {
1235 bp->b_error = EROFS;
1236 goto error;
1237 }
1238#endif
1239
1240 /*
1241 * Check for out of bounds, EOF, and EOF clipping.
1242 */
1243 if (bio->bio_offset < 0)
1244 goto bad;
1245 if (blkno + sz > maxsz) {
1246 /*
1247 * Past EOF or B_BNOCLIP flag was set, the request is bad.
1248 */
1249 if (blkno > maxsz || (bp->b_flags & B_BNOCLIP))
1250 goto bad;
1251
1252 /*
1253 * If exactly on EOF just complete the I/O with no bytes
1254 * transfered. B_INVAL must be set to throw away the
1255 * contents of the buffer. Otherwise clip b_bcount.
1256 */
1257 if (blkno == maxsz) {
1258 bp->b_resid = bp->b_bcount;
1259 bp->b_flags |= B_INVAL;
1260 goto done;
1261 }
1262 bp->b_bcount = (maxsz - blkno) << DEV_BSHIFT;
1263 }
1264 nbio = push_bio(bio);
1265 nbio->bio_offset = bio->bio_offset + ((off_t)p->p_offset << DEV_BSHIFT);
1266 return (nbio);
1267
1268 /*
1269 * The caller is responsible for calling biodone() on the passed bio
1270 * when we return NULL.
1271 */
1272bad:
1273 bp->b_error = EINVAL;
1274error:
1275 bp->b_resid = bp->b_bcount;
1276 bp->b_flags |= B_ERROR | B_INVAL;
1277done:
1278 return (NULL);
1279}
1280