]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/i386/stubs.c
512c730b1107dfae05e9f98f7eb7597dc8215afc
[apple/xnu.git] / bsd / dev / i386 / stubs.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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@
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>
38 #include <sys/ioctl.h>
39 #include <sys/tty.h>
40 #include <sys/conf.h>
41 #include <sys/kauth.h>
42 #include <sys/ucred.h>
43 #include <sys/proc_internal.h>
44 #include <sys/user.h>
45 #include <kern/task.h>
46 #include <kern/thread.h>
47 #include <vm/vm_map.h>
48 #include <machine/machine_routines.h>
49
50 /* XXX should be elsewhere (cpeak) */
51 extern struct proc *i386_current_proc(void);
52 extern void *get_bsduthreadarg(thread_t);
53 extern int *get_bsduthreadrval(thread_t);
54 extern void *find_user_regs(thread_t);
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 */
66 int
67 copyoutstr(const void *from, user_addr_t to, size_t maxlen, size_t *lencopied)
68 {
69 size_t slen;
70 size_t len;
71 int error = 0;
72
73 slen = strlen(from) + 1;
74 if (slen > maxlen)
75 error = ENAMETOOLONG;
76
77 len = min(maxlen,slen);
78 if (copyout(from, to, len))
79 error = EFAULT;
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 */
97 int
98 copystr(const void *vfrom, void *vto, size_t maxlen, size_t *lencopied)
99 {
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;
114 }
115
116 int
117 copywithin(void *src, void *dst, size_t count)
118 {
119 bcopy(src,dst,count);
120 return 0;
121 }
122
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 */
131 struct proc *
132 i386_current_proc(void)
133 {
134 struct uthread * ut;
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 }
150 }
151
152 /* Not in vfork - may return NULL */
153 p = (struct proc *)get_bsdtask_info(current_task());
154
155 return (p);
156 }
157
158 void *
159 get_bsduthreadarg(thread_t th)
160 {
161 void *arg_ptr;
162 struct uthread *ut;
163
164 ut = get_bsdthread_info(th);
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);
172 }
173
174 int *
175 get_bsduthreadrval(thread_t th)
176 {
177 struct uthread *ut;
178
179 ut = get_bsdthread_info(th);
180 return(&ut->uu_rval[0]);
181 }