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