]> git.saurik.com Git - apple/xnu.git/blame - bsd/dev/i386/stubs.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / bsd / dev / i386 / stubs.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
8ad349bb 4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
1c79356b 5 *
8ad349bb
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
1c79356b
A
29 */
30/*
31 * Copyright (c) 1997 by Apple Computer, Inc., all rights reserved
32 * Copyright (c) 1993 NeXT Computer, Inc.
33 *
34 */
35
36#include <sys/param.h>
37#include <sys/systm.h>
1c79356b
A
38#include <sys/ioctl.h>
39#include <sys/tty.h>
40#include <sys/conf.h>
91447636
A
41#include <sys/kauth.h>
42#include <sys/ucred.h>
43#include <sys/proc_internal.h>
1c79356b
A
44#include <sys/user.h>
45#include <kern/task.h>
46#include <kern/thread.h>
1c79356b 47#include <vm/vm_map.h>
5d5c5d0d 48#include <machine/machine_routines.h>
1c79356b 49
91447636 50/* XXX should be elsewhere (cpeak) */
5d5c5d0d 51extern struct proc *i386_current_proc(void);
91447636
A
52extern void *get_bsduthreadarg(thread_t);
53extern int *get_bsduthreadrval(thread_t);
5d5c5d0d 54extern void *find_user_regs(thread_t);
1c79356b
A
55
56/*
57 * copy a null terminated string from the kernel address space into
58 * the user address space.
59 * - if the user is denied write access, return EFAULT.
60 * - if the end of string isn't found before
61 * maxlen bytes are copied, return ENAMETOOLONG,
62 * indicating an incomplete copy.
63 * - otherwise, return 0, indicating success.
64 * the number of bytes copied is always returned in lencopied.
65 */
66int
91447636 67copyoutstr(const void *from, user_addr_t to, size_t maxlen, size_t *lencopied)
1c79356b 68{
91447636
A
69 size_t slen;
70 size_t len;
71 int error = 0;
1c79356b
A
72
73 slen = strlen(from) + 1;
55e303ae
A
74 if (slen > maxlen)
75 error = ENAMETOOLONG;
1c79356b
A
76
77 len = min(maxlen,slen);
78 if (copyout(from, to, len))
55e303ae 79 error = EFAULT;
1c79356b
A
80 *lencopied = len;
81
82 return error;
83}
84
85
86/*
87 * copy a null terminated string from one point to another in
88 * the kernel address space.
89 * - no access checks are performed.
90 * - if the end of string isn't found before
91 * maxlen bytes are copied, return ENAMETOOLONG,
92 * indicating an incomplete copy.
93 * - otherwise, return 0, indicating success.
94 * the number of bytes copied is always returned in lencopied.
95 */
96/* from ppc/fault_copy.c -Titan1T4 VERSION */
97int
91447636 98copystr(const void *vfrom, void *vto, size_t maxlen, size_t *lencopied)
1c79356b 99{
91447636
A
100 size_t l;
101 char const *from = (char const *) vfrom;
102 char *to = (char *) vto;
103
104 for (l = 0; l < maxlen; l++) {
105 if ((*to++ = *from++) == '\0') {
106 if (lencopied)
107 *lencopied = l + 1;
108 return 0;
109 }
110 }
111 if (lencopied)
112 *lencopied = maxlen;
113 return ENAMETOOLONG;
1c79356b
A
114}
115
91447636
A
116int
117copywithin(void *src, void *dst, size_t count)
1c79356b
A
118{
119 bcopy(src,dst,count);
120 return 0;
121}
122
5d5c5d0d
A
123/*
124 * This is just current_proc() from bsd/kern/bsd_stubs.c, but instead of
125 * returning kernproc in the non-vfork() case, it can return NULL. This is
126 * needed because the system call entry point is in osfmk/i386/bsd_i386.c
127 * instead of bsd/dev/i386, and therefore cannot see some BSD thread
128 * internals. We need to distinguish kernproc defaulting in the vfork and
129 * non-vfork cases vs. actually being the real process context.
130 */
131struct proc *
132i386_current_proc(void)
133{
91447636 134 struct uthread * ut;
5d5c5d0d
A
135 struct proc *p;
136 thread_t thr_act = current_thread();
137
138 ut = (struct uthread *)get_bsdthread_info(thr_act);
139 if (ut && (ut->uu_flag & UT_VFORK)) {
140 if (ut->uu_proc) {
141 p = ut->uu_proc;
142 if ((p->p_flag & P_INVFORK) == 0)
143 panic("returning child proc not under vfork");
144 if (p->p_vforkact != (void *)thr_act)
145 panic("returning child proc which is not cur_act");
146 return(p);
147 } else {
148 return (kernproc);
149 }
8ad349bb 150 }
c0fea474 151
5d5c5d0d
A
152 /* Not in vfork - may return NULL */
153 p = (struct proc *)get_bsdtask_info(current_task());
154
155 return (p);
1c79356b
A
156}
157
158void *
159get_bsduthreadarg(thread_t th)
160{
5d5c5d0d
A
161 void *arg_ptr;
162 struct uthread *ut;
163
1c79356b 164 ut = get_bsdthread_info(th);
5d5c5d0d
A
165
166 if (ml_thread_is64bit(th) == TRUE)
167 arg_ptr = (void *)saved_state64(find_user_regs(th));
168 else
169 arg_ptr = (void *)(ut->uu_arg);
170
171 return(arg_ptr);
1c79356b
A
172}
173
174int *
91447636 175get_bsduthreadrval(thread_t th)
1c79356b 176{
5d5c5d0d 177 struct uthread *ut;
8ad349bb 178
8ad349bb 179 ut = get_bsdthread_info(th);
5d5c5d0d 180 return(&ut->uu_rval[0]);
8ad349bb 181}