xref: /f-stack/lib/ff_compat.c (revision c36e692a)
1 /*
2  * Copyright (c) 2010 Kip Macy. All rights reserved.
3  * Copyright (C) 2017 THL A29 Limited, a Tencent company.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  *   list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  *   this list of conditions and the following disclaimer in the documentation
13  *   and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * Derived in part from libplebnet's pn_compat.c.
27  *
28  */
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/kthread.h>
33 #include <sys/refcount.h>
34 #include <sys/stat.h>
35 #include <sys/stdint.h>
36 #include <sys/time.h>
37 #include <sys/ucred.h>
38 #include <sys/uio.h>
39 #include <sys/proc.h>
40 #include <sys/tty.h>
41 #include <sys/sx.h>
42 #include <sys/linker.h>
43 #include <sys/racct.h>
44 #include <sys/malloc.h>
45 #include <sys/syscallsubr.h>
46 #include <sys/libkern.h>
47 #include <sys/random.h>
48 #include <sys/mman.h>
49 #include <sys/vdso.h>
50 
51 #include <machine/elf.h>
52 #include <machine/md_var.h>
53 
54 #include "ff_host_interface.h"
55 
56 TAILQ_HEAD(prisonlist, prison);
57 
58 __thread struct thread *pcurthread;
59 
60 struct cdev;
61 struct vnode *rootvnode;
62 extern struct proc proc0;
63 struct proclist allproc;
64 struct sx allproc_lock;
65 struct sx allprison_lock;
66 struct prisonlist allprison;
67 
68 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
69 int async_io_version;
70 
71 #define M_ZERO        0x0100        /* bzero the allocation */
72 
73 int vttoif_tab[10] = {
74     0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
75     S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
76 };
77 
78 void ff_init_thread0(void);
79 
80 void
81 resettodr(void)
82 {
83 
84 }
85 
86 void
87 ff_init_thread0(void)
88 {
89     pcurthread = &thread0;
90 }
91 
92 int
93 kproc_kthread_add(void (*start_routine)(void *), void *arg,
94     struct proc **p,  struct thread **tdp,
95     int flags, int pages,
96     const char *procname, const char *str, ...)
97 {
98     return 0;
99 }
100 
101 int
102 kthread_add(void (*start_routine)(void *), void *arg, struct proc *p,
103     struct thread **tdp, int flags, int pages,
104     const char *str, ...)
105 {
106     return 0;
107 }
108 
109 void
110 kthread_exit(void)
111 {
112     panic("kthread_exit unsupported");
113 }
114 
115 void
116 tdsignal(struct thread *td, int sig)
117 {
118     return;
119 }
120 
121 dev_t
122 tty_udev(struct tty *tp)
123 {
124     return (NODEV);
125 }
126 
127 int
128 p_candebug(struct thread *td, struct proc *p)
129 {
130     return (0);
131 }
132 
133 const char *
134 devtoname(struct cdev *dev)
135 {
136     return (NULL);
137 }
138 
139 #ifdef RACCT
140 uint64_t
141 racct_get_limit(struct proc *p, int resource)
142 {
143     return (UINT64_MAX);
144 }
145 #endif
146 
147 int
148 kern_openat(struct thread *td, int fd, char *path, enum uio_seg pathseg,
149     int flags, int mode)
150 {
151     return (-1);
152 }
153 
154 /* Process one elf relocation with addend. */
155 static int
156 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
157     int type, int local, elf_lookup_fn lookup)
158 {
159     Elf64_Addr *where, val;
160     Elf32_Addr *where32, val32;
161     Elf_Addr addr;
162     Elf_Addr addend;
163     Elf_Size rtype, symidx;
164     const Elf_Rel *rel;
165     const Elf_Rela *rela;
166     int error;
167 
168     switch (type) {
169         case ELF_RELOC_REL:
170             rel = (const Elf_Rel *)data;
171             where = (Elf_Addr *) (relocbase + rel->r_offset);
172             rtype = ELF_R_TYPE(rel->r_info);
173             symidx = ELF_R_SYM(rel->r_info);
174             /* Addend is 32 bit on 32 bit relocs */
175             switch (rtype) {
176                 case R_X86_64_PC32:
177                 case R_X86_64_32S:
178                     addend = *(Elf32_Addr *)where;
179                     break;
180                 default:
181                     addend = *where;
182                     break;
183             }
184             break;
185         case ELF_RELOC_RELA:
186             rela = (const Elf_Rela *)data;
187             where = (Elf_Addr *) (relocbase + rela->r_offset);
188             addend = rela->r_addend;
189             rtype = ELF_R_TYPE(rela->r_info);
190             symidx = ELF_R_SYM(rela->r_info);
191             break;
192         default:
193             panic("unknown reloc type %d\n", type);
194     }
195 
196     switch (rtype) {
197         case R_X86_64_NONE:    /* none */
198             break;
199 
200         case R_X86_64_64:        /* S + A */
201             error = lookup(lf, symidx, 1, &addr);
202             val = addr + addend;
203             if (error != 0)
204                 return -1;
205             if (*where != val)
206                 *where = val;
207             break;
208 
209         case R_X86_64_PC32:    /* S + A - P */
210             error = lookup(lf, symidx, 1, &addr);
211             where32 = (Elf32_Addr *)where;
212             val32 = (Elf32_Addr)(addr + addend - (Elf_Addr)where);
213             if (error != 0)
214                 return -1;
215             if (*where32 != val32)
216                 *where32 = val32;
217             break;
218 
219         case R_X86_64_32S:    /* S + A sign extend */
220             error = lookup(lf, symidx, 1, &addr);
221             val32 = (Elf32_Addr)(addr + addend);
222             where32 = (Elf32_Addr *)where;
223             if (error != 0)
224                 return -1;
225             if (*where32 != val32)
226                 *where32 = val32;
227             break;
228 
229         case R_X86_64_COPY:    /* none */
230             /*
231              * There shouldn't be copy relocations in kernel
232              * objects.
233              */
234             printf("kldload: unexpected R_COPY relocation\n");
235             return -1;
236             break;
237 
238         case R_X86_64_GLOB_DAT:    /* S */
239         case R_X86_64_JMP_SLOT:    /* XXX need addend + offset */
240             error = lookup(lf, symidx, 1, &addr);
241             if (error != 0)
242                 return -1;
243             if (*where != addr)
244                 *where = addr;
245             break;
246 
247         case R_X86_64_RELATIVE:    /* B + A */
248             addr = relocbase + addend;
249             val = addr;
250             if (*where != val)
251                 *where = val;
252             break;
253 
254         default:
255             printf("kldload: unexpected relocation type %ld\n",
256                    rtype);
257             return -1;
258     }
259     return(0);
260 }
261 
262 int
263 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
264     elf_lookup_fn lookup)
265 {
266     return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
267 }
268 
269 int
270 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
271     int type, elf_lookup_fn lookup)
272 {
273     return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
274 }
275 
276 int
277 elf_cpu_load_file(linker_file_t lf __unused)
278 {
279     return (0);
280 }
281 
282 int
283 elf_cpu_unload_file(linker_file_t lf __unused)
284 {
285     return (0);
286 }
287 
288 void
289 arc4rand(void *ptr, unsigned int len, int reseed)
290 {
291     ff_arc4rand(ptr, len, reseed);
292 }
293 
294 uint32_t
295 arc4random(void)
296 {
297     return ff_arc4random();
298 }
299 
300 void
301 random_harvest_queue(const void *entropy, u_int size,
302     u_int bits, enum random_entropy_source origin)
303 {
304     ;
305 }
306 
307 u_int
308 read_random(void *buf, u_int count)
309 {
310     arc4rand(buf, count, 0);
311     return (count);
312 }
313 
314 int
315 fubyte(volatile const void *base)
316 {
317     return (*(volatile const uint8_t *)base);
318 }
319 
320 int
321 fueword(volatile const void *base, long *val)
322 {
323     *val = (*(volatile const long *)base);
324     return 0;
325 }
326 
327 void
328 timekeep_push_vdso(void)
329 {
330     ;
331 }
332 
333 uint32_t
334 cpu_fill_vdso_timehands(struct vdso_timehands *vdso_th, struct timecounter *tc)
335 {
336     return (0);
337 }
338 
339