]> git.saurik.com Git - apple/xnu.git/blob - bsd/dev/i386/stubs.c
f4a56c042d455856c84a780f3ab68e90c25687a2
[apple/xnu.git] / bsd / dev / i386 / stubs.c
1 /*
2 * Copyright (c) 2006 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
49 /* XXX should be elsewhere (cpeak) */
50 extern int set_bsduthreadargs(thread_t, void *, void *);
51 extern void *get_bsduthreadarg(thread_t);
52 extern int *get_bsduthreadrval(thread_t);
53 extern int *get_bsduthreadlowpridelay(thread_t);
54
55 /*
56 * copy a null terminated string from the kernel address space into
57 * the user address space.
58 * - if the user is denied write access, return EFAULT.
59 * - if the end of string isn't found before
60 * maxlen bytes are copied, return ENAMETOOLONG,
61 * indicating an incomplete copy.
62 * - otherwise, return 0, indicating success.
63 * the number of bytes copied is always returned in lencopied.
64 */
65 int
66 copyoutstr(const void *from, user_addr_t to, size_t maxlen, size_t *lencopied)
67 {
68 size_t slen;
69 size_t len;
70 int error = 0;
71
72 slen = strlen(from) + 1;
73 if (slen > maxlen)
74 error = ENAMETOOLONG;
75
76 len = min(maxlen,slen);
77 if (copyout(from, to, len))
78 error = EFAULT;
79 *lencopied = len;
80
81 return error;
82 }
83
84
85 /*
86 * copy a null terminated string from one point to another in
87 * the kernel address space.
88 * - no access checks are performed.
89 * - if the end of string isn't found before
90 * maxlen bytes are copied, return ENAMETOOLONG,
91 * indicating an incomplete copy.
92 * - otherwise, return 0, indicating success.
93 * the number of bytes copied is always returned in lencopied.
94 */
95 /* from ppc/fault_copy.c -Titan1T4 VERSION */
96 int
97 copystr(const void *vfrom, void *vto, size_t maxlen, size_t *lencopied)
98 {
99 size_t l;
100 char const *from = (char const *) vfrom;
101 char *to = (char *) vto;
102
103 for (l = 0; l < maxlen; l++) {
104 if ((*to++ = *from++) == '\0') {
105 if (lencopied)
106 *lencopied = l + 1;
107 return 0;
108 }
109 }
110 if (lencopied)
111 *lencopied = maxlen;
112 return ENAMETOOLONG;
113 }
114
115 int
116 copywithin(void *src, void *dst, size_t count)
117 {
118 bcopy(src,dst,count);
119 return 0;
120 }
121
122 int
123 set_bsduthreadargs(thread_t th, void * pcb, __unused void *ignored_arg)
124 {
125 struct uthread * ut;
126 struct proc *p = current_proc();
127
128 ut = get_bsdthread_info(th);
129 ut->uu_ar0 = (int *)pcb;
130
131 /*
132 * Delayed binding of thread credential to process credential.
133 *
134 * XXX This doesn't really belong here, but the i386 code has a
135 * XXX number of seemingly gratuitous structural differences that
136 * XXX make this the most appropriate place to do the work.
137 */
138 if (ut->uu_ucred != p->p_ucred &&
139 (ut->uu_flag & UT_SETUID) == 0) {
140 kauth_cred_t old = ut->uu_ucred;
141 proc_lock(p);
142 ut->uu_ucred = p->p_ucred;
143 kauth_cred_ref(ut->uu_ucred);
144 proc_unlock(p);
145 if (old != NOCRED)
146 kauth_cred_rele(old);
147 }
148
149 return(1);
150 }
151
152 void *
153 get_bsduthreadarg(thread_t th)
154 {
155 struct uthread *ut;
156 ut = get_bsdthread_info(th);
157 return((void *)(ut->uu_arg));
158 }
159
160 int *
161 get_bsduthreadrval(thread_t th)
162 {
163 struct uthread *ut;
164 ut = get_bsdthread_info(th);
165 return(&ut->uu_rval[0]);
166 }
167
168 int *
169 get_bsduthreadlowpridelay(thread_t th)
170 {
171 struct uthread *ut;
172 ut = get_bsdthread_info(th);
173 return(&ut->uu_lowpri_delay);
174 }