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