xref: /f-stack/lib/ff_glue.c (revision 02610d58)
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_glue.c.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/kernel.h>
33 #include <sys/kthread.h>
34 #include <sys/event.h>
35 #include <sys/jail.h>
36 #include <sys/limits.h>
37 #include <sys/malloc.h>
38 #include <sys/refcount.h>
39 #include <sys/resourcevar.h>
40 #include <sys/sysctl.h>
41 #include <sys/sysent.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/priv.h>
45 #include <sys/time.h>
46 #include <sys/ucred.h>
47 #include <sys/uio.h>
48 #include <sys/param.h>
49 #include <sys/bus.h>
50 #include <sys/buf.h>
51 #include <sys/file.h>
52 #include <sys/vmem.h>
53 #include <sys/mbuf.h>
54 #include <sys/smp.h>
55 #include <sys/sched.h>
56 
57 #include <vm/vm.h>
58 #include <vm/vm_param.h>
59 #include <vm/pmap.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_extern.h>
63 
64 #include "ff_host_interface.h"
65 
66 int kstack_pages = KSTACK_PAGES;
67 SYSCTL_INT(_kern, OID_AUTO, kstack_pages, CTLFLAG_RD, &kstack_pages, 0,
68     "Kernel stack size in pages");
69 
70 int bootverbose;
71 
72 SYSCTL_ROOT_NODE(0, sysctl, CTLFLAG_RW, 0, "Sysctl internal magic");
73 
74 SYSCTL_ROOT_NODE(CTL_VFS, vfs, CTLFLAG_RW, 0, "File system");
75 
76 SYSCTL_ROOT_NODE(CTL_KERN, kern, CTLFLAG_RW, 0, "High kernel, proc, limits &c");
77 
78 SYSCTL_ROOT_NODE(CTL_NET, net, CTLFLAG_RW, 0, "Network, (see socket.h)");
79 
80 SYSCTL_ROOT_NODE(CTL_MACHDEP, machdep, CTLFLAG_RW, 0, "machine dependent");
81 
82 SYSCTL_ROOT_NODE(CTL_VM, vm, CTLFLAG_RW, 0, "Virtual memory");
83 
84 SYSCTL_ROOT_NODE(CTL_DEBUG, debug, CTLFLAG_RW, 0, "Debugging");
85 
86 SYSCTL_ROOT_NODE(OID_AUTO, security, CTLFLAG_RW, 0, "Security");
87 
88 SYSCTL_NODE(_kern, OID_AUTO, features, CTLFLAG_RD, 0, "Kernel Features");
89 
90 SYSCTL_NODE(_kern, KERN_PROC, proc, CTLFLAG_RD,  0, "Process table");
91 
92 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
93 MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
94 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
95 static MALLOC_DEFINE(M_PLIMIT, "plimit", "plimit structures");
96 
97 MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
98 MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
99 
100 static void configure_final(void *dummy);
101 
102 SYSINIT(configure3, SI_SUB_CONFIGURE, SI_ORDER_ANY, configure_final, NULL);
103 
104 volatile int ticks;
105 int cpu_disable_deep_sleep;
106 
107 static int sysctl_kern_smp_active(SYSCTL_HANDLER_ARGS);
108 
109 /* This is used in modules that need to work in both SMP and UP. */
110 cpuset_t all_cpus;
111 
112 int mp_ncpus;
113 /* export this for libkvm consumers. */
114 int mp_maxcpus = MAXCPU;
115 
116 volatile int smp_started;
117 u_int mp_maxid;
118 
119 static SYSCTL_NODE(_kern, OID_AUTO, smp, CTLFLAG_RD|CTLFLAG_CAPRD, NULL,
120     "Kernel SMP");
121 
122 SYSCTL_INT(_kern_smp, OID_AUTO, maxid, CTLFLAG_RD|CTLFLAG_CAPRD, &mp_maxid, 0,
123     "Max CPU ID.");
124 
125 SYSCTL_INT(_kern_smp, OID_AUTO, maxcpus, CTLFLAG_RD|CTLFLAG_CAPRD, &mp_maxcpus,
126     0, "Max number of CPUs that the system was compiled for.");
127 
128 SYSCTL_PROC(_kern_smp, OID_AUTO, active, CTLFLAG_RD | CTLTYPE_INT, NULL, 0,
129     sysctl_kern_smp_active, "I", "Indicates system is running in SMP mode");
130 
131 int smp_disabled = 0;	/* has smp been disabled? */
132 SYSCTL_INT(_kern_smp, OID_AUTO, disabled, CTLFLAG_RDTUN|CTLFLAG_CAPRD,
133     &smp_disabled, 0, "SMP has been disabled from the loader");
134 
135 int smp_cpus = 1;	/* how many cpu's running */
136 SYSCTL_INT(_kern_smp, OID_AUTO, cpus, CTLFLAG_RD|CTLFLAG_CAPRD, &smp_cpus, 0,
137     "Number of CPUs online");
138 
139 int smp_topology = 0;	/* Which topology we're using. */
140 SYSCTL_INT(_kern_smp, OID_AUTO, topology, CTLFLAG_RDTUN, &smp_topology, 0,
141     "Topology override setting; 0 is default provided by hardware.");
142 
143 
144 long first_page = 0;
145 
146 struct vmmeter vm_cnt;
147 vm_map_t kernel_map=0;
148 vm_map_t kmem_map=0;
149 
150 vmem_t *kernel_arena = NULL;
151 vmem_t *kmem_arena = NULL;
152 
153 struct vm_object kernel_object_store;
154 struct vm_object kmem_object_store;
155 
156 struct filterops fs_filtops;
157 struct filterops sig_filtops;
158 
159 int cold = 1;
160 
161 int unmapped_buf_allowed = 1;
162 
163 int cpu_deepest_sleep = 0;    /* Deepest Cx state available. */
164 int cpu_disable_c2_sleep = 0; /* Timer dies in C2. */
165 int cpu_disable_c3_sleep = 0; /* Timer dies in C3. */
166 
167 static void timevalfix(struct timeval *);
168 
169 /* Extra care is taken with this sysctl because the data type is volatile */
170 static int
171 sysctl_kern_smp_active(SYSCTL_HANDLER_ARGS)
172 {
173 	int error, active;
174 
175 	active = smp_started;
176 	error = SYSCTL_OUT(req, &active, sizeof(active));
177 	return (error);
178 }
179 
180 void
181 procinit()
182 {
183     sx_init(&allproc_lock, "allproc");
184     LIST_INIT(&allproc);
185 }
186 
187 
188 /*
189  * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
190  */
191 struct prison *
192 prison_find_child(struct prison *mypr, int prid)
193 {
194     return (NULL);
195 }
196 
197 void
198 prison_free(struct prison *pr)
199 {
200 
201 }
202 
203 void
204 prison_hold_locked(struct prison *pr)
205 {
206 
207 }
208 
209 int
210 prison_if(struct ucred *cred, struct sockaddr *sa)
211 {
212     return (0);
213 }
214 
215 int
216 prison_check_af(struct ucred *cred, int af)
217 {
218     return (0);
219 }
220 
221 int
222 prison_check_ip4(const struct ucred *cred, const struct in_addr *ia)
223 {
224     return (0);
225 }
226 
227 int
228 prison_equal_ip4(struct prison *pr1, struct prison *pr2)
229 {
230     return (1);
231 }
232 
233 #ifdef INET6
234 int
235 prison_check_ip6(struct ucred *cred, struct in6_addr *ia)
236 {
237     return (0);
238 }
239 
240 int
241 prison_equal_ip6(struct prison *pr1, struct prison *pr2)
242 {
243     return (1);
244 }
245 #endif
246 
247 /*
248  * See if a prison has the specific flag set.
249  */
250 int
251 prison_flag(struct ucred *cred, unsigned flag)
252 {
253     /* This is an atomic read, so no locking is necessary. */
254     return (flag & PR_HOST);
255 }
256 
257 int
258 prison_get_ip4(struct ucred *cred, struct in_addr *ia)
259 {
260     return (0);
261 }
262 
263 int
264 prison_local_ip4(struct ucred *cred, struct in_addr *ia)
265 {
266     return (0);
267 }
268 
269 int
270 prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
271 {
272     return (0);
273 }
274 
275 #ifdef INET6
276 int
277 prison_get_ip6(struct ucred *cred, struct in6_addr *ia)
278 {
279     return (0);
280 }
281 
282 int
283 prison_local_ip6(struct ucred *cred, struct in6_addr *ia, int other)
284 {
285     return (0);
286 }
287 
288 int
289 prison_remote_ip6(struct ucred *cred, struct in6_addr *ia)
290 {
291     return (0);
292 }
293 #endif
294 
295 int
296 prison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
297 {
298     /* not jailed */
299     return (1);
300 }
301 
302 #ifdef INET6
303 int
304 prison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia)
305 {
306     /* not jailed */
307     return (1);
308 }
309 #endif
310 
311 int
312 jailed(struct ucred *cred)
313 {
314     return (0);
315 }
316 
317 /*
318  * Return 1 if the passed credential is in a jail and that jail does not
319  * have its own virtual network stack, otherwise 0.
320  */
321 int
322 jailed_without_vnet(struct ucred *cred)
323 {
324     return (0);
325 }
326 
327 int
328 priv_check(struct thread *td, int priv)
329 {
330     return (0);
331 }
332 
333 int
334 priv_check_cred(struct ucred *cred, int priv, int flags)
335 {
336     return (0);
337 }
338 
339 
340 int
341 vslock(void *addr, size_t len)
342 {
343     return (0);
344 }
345 
346 void
347 vsunlock(void *addr, size_t len)
348 {
349 
350 }
351 
352 
353 /*
354  * Check that a proposed value to load into the .it_value or
355  * .it_interval part of an interval timer is acceptable, and
356  * fix it to have at least minimal value (i.e. if it is less
357  * than the resolution of the clock, round it up.)
358  */
359 int
360 itimerfix(struct timeval *tv)
361 {
362 
363     if (tv->tv_sec < 0 || tv->tv_usec < 0 || tv->tv_usec >= 1000000)
364         return (EINVAL);
365     if (tv->tv_sec == 0 && tv->tv_usec != 0 && tv->tv_usec < tick)
366         tv->tv_usec = tick;
367     return (0);
368 }
369 
370 /*
371  * Decrement an interval timer by a specified number
372  * of microseconds, which must be less than a second,
373  * i.e. < 1000000.  If the timer expires, then reload
374  * it.  In this case, carry over (usec - old value) to
375  * reduce the value reloaded into the timer so that
376  * the timer does not drift.  This routine assumes
377  * that it is called in a context where the timers
378  * on which it is operating cannot change in value.
379  */
380 int
381 itimerdecr(struct itimerval *itp, int usec)
382 {
383     if (itp->it_value.tv_usec < usec) {
384         if (itp->it_value.tv_sec == 0) {
385             /* expired, and already in next interval */
386             usec -= itp->it_value.tv_usec;
387             goto expire;
388         }
389         itp->it_value.tv_usec += 1000000;
390         itp->it_value.tv_sec--;
391     }
392     itp->it_value.tv_usec -= usec;
393     usec = 0;
394     if (timevalisset(&itp->it_value))
395         return (1);
396     /* expired, exactly at end of interval */
397 expire:
398     if (timevalisset(&itp->it_interval)) {
399         itp->it_value = itp->it_interval;
400         itp->it_value.tv_usec -= usec;
401         if (itp->it_value.tv_usec < 0) {
402             itp->it_value.tv_usec += 1000000;
403             itp->it_value.tv_sec--;
404         }
405     } else
406         itp->it_value.tv_usec = 0;        /* sec is already 0 */
407     return (0);
408 }
409 
410 /*
411  * Add and subtract routines for timevals.
412  * N.B.: subtract routine doesn't deal with
413  * results which are before the beginning,
414  * it just gets very confused in this case.
415  * Caveat emptor.
416  */
417 void
418 timevaladd(struct timeval *t1, const struct timeval *t2)
419 {
420     t1->tv_sec += t2->tv_sec;
421     t1->tv_usec += t2->tv_usec;
422     timevalfix(t1);
423 }
424 
425 void
426 timevalsub(struct timeval *t1, const struct timeval *t2)
427 {
428     t1->tv_sec -= t2->tv_sec;
429     t1->tv_usec -= t2->tv_usec;
430     timevalfix(t1);
431 }
432 
433 static void
434 timevalfix(struct timeval *t1)
435 {
436     if (t1->tv_usec < 0) {
437         t1->tv_sec--;
438         t1->tv_usec += 1000000;
439     }
440     if (t1->tv_usec >= 1000000) {
441         t1->tv_sec++;
442         t1->tv_usec -= 1000000;
443     }
444 }
445 
446 /*
447  * ratecheck(): simple time-based rate-limit checking.
448  */
449 int
450 ratecheck(struct timeval *lasttime, const struct timeval *mininterval)
451 {
452     struct timeval tv, delta;
453     int rv = 0;
454 
455     getmicrouptime(&tv);        /* NB: 10ms precision */
456     delta = tv;
457     timevalsub(&delta, lasttime);
458 
459     /*
460      * check for 0,0 is so that the message will be seen at least once,
461      * even if interval is huge.
462      */
463     if (timevalcmp(&delta, mininterval, >=) ||
464         (lasttime->tv_sec == 0 && lasttime->tv_usec == 0)) {
465         *lasttime = tv;
466         rv = 1;
467     }
468 
469     return (rv);
470 }
471 
472 /*
473  * ppsratecheck(): packets (or events) per second limitation.
474  *
475  * Return 0 if the limit is to be enforced (e.g. the caller
476  * should drop a packet because of the rate limitation).
477  *
478  * maxpps of 0 always causes zero to be returned.  maxpps of -1
479  * always causes 1 to be returned; this effectively defeats rate
480  * limiting.
481  *
482  * Note that we maintain the struct timeval for compatibility
483  * with other bsd systems.  We reuse the storage and just monitor
484  * clock ticks for minimal overhead.
485  */
486 int
487 ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
488 {
489     int now;
490 
491     /*
492      * Reset the last time and counter if this is the first call
493      * or more than a second has passed since the last update of
494      * lasttime.
495      */
496     now = ticks;
497     if (lasttime->tv_sec == 0 || (u_int)(now - lasttime->tv_sec) >= hz) {
498         lasttime->tv_sec = now;
499         *curpps = 1;
500         return (maxpps != 0);
501     } else {
502         (*curpps)++;        /* NB: ignore potential overflow */
503         return (maxpps < 0 || *curpps < maxpps);
504     }
505 }
506 
507 /*
508  * Compute number of ticks in the specified amount of time.
509  */
510 int
511 tvtohz(tv)
512     struct timeval *tv;
513 {
514     register unsigned long ticks;
515     register long sec, usec;
516 
517     /*
518      * If the number of usecs in the whole seconds part of the time
519      * difference fits in a long, then the total number of usecs will
520      * fit in an unsigned long.  Compute the total and convert it to
521      * ticks, rounding up and adding 1 to allow for the current tick
522      * to expire.  Rounding also depends on unsigned long arithmetic
523      * to avoid overflow.
524      *
525      * Otherwise, if the number of ticks in the whole seconds part of
526      * the time difference fits in a long, then convert the parts to
527      * ticks separately and add, using similar rounding methods and
528      * overflow avoidance.  This method would work in the previous
529      * case but it is slightly slower and assumes that hz is integral.
530      *
531      * Otherwise, round the time difference down to the maximum
532      * representable value.
533      *
534      * If ints have 32 bits, then the maximum value for any timeout in
535      * 10ms ticks is 248 days.
536      */
537     sec = tv->tv_sec;
538     usec = tv->tv_usec;
539     if (usec < 0) {
540         sec--;
541         usec += 1000000;
542     }
543     if (sec < 0) {
544 #ifdef DIAGNOSTIC
545         if (usec > 0) {
546             sec++;
547             usec -= 1000000;
548         }
549         printf("tvotohz: negative time difference %ld sec %ld usec\n",
550                sec, usec);
551 #endif
552         ticks = 1;
553     } else if (sec <= LONG_MAX / 1000000)
554         ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
555             / tick + 1;
556     else if (sec <= LONG_MAX / hz)
557         ticks = sec * hz
558             + ((unsigned long)usec + (tick - 1)) / tick + 1;
559     else
560         ticks = LONG_MAX;
561     if (ticks > INT_MAX)
562         ticks = INT_MAX;
563     return ((int)ticks);
564 }
565 
566 int
567 copyin(const void *uaddr, void *kaddr, size_t len)
568 {
569     memcpy(kaddr, uaddr, len);
570     return (0);
571 }
572 
573 int
574 copyout(const void *kaddr, void *uaddr, size_t len)
575 {
576     memcpy(uaddr, kaddr, len);
577     return (0);
578 }
579 
580 int
581 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
582 {
583     size_t bytes;
584 
585     bytes = strlcpy(kdaddr, kfaddr, len);
586     if (done != NULL)
587         *done = bytes;
588 
589     return (0);
590 }
591 
592 
593 
594 int
595 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
596 {
597     size_t bytes;
598 
599     bytes = strlcpy(kaddr, uaddr, len);
600     if (done != NULL)
601         *done = bytes;
602 
603     return (0);
604 }
605 
606 int
607 copyiniov(const struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
608 {
609     u_int iovlen;
610 
611     *iov = NULL;
612     if (iovcnt > UIO_MAXIOV)
613         return (error);
614     iovlen = iovcnt * sizeof (struct iovec);
615     *iov = malloc(iovlen, M_IOV, M_WAITOK);
616     error = copyin(iovp, *iov, iovlen);
617     if (error) {
618         free(*iov, M_IOV);
619         *iov = NULL;
620     }
621     return (error);
622 }
623 
624 int
625 subyte(volatile void *base, int byte)
626 {
627     *(volatile char *)base = (uint8_t)byte;
628     return (0);
629 }
630 
631 static inline int
632 chglimit(struct uidinfo *uip, long *limit, int diff, rlim_t max, const char *name)
633 {
634     /* Don't allow them to exceed max, but allow subtraction. */
635     if (diff > 0 && max != 0) {
636         if (atomic_fetchadd_long(limit, (long)diff) + diff > max) {
637             atomic_subtract_long(limit, (long)diff);
638             return (0);
639         }
640     } else {
641         atomic_add_long(limit, (long)diff);
642         if (*limit < 0)
643             printf("negative %s for uid = %d\n", name, uip->ui_uid);
644     }
645     return (1);
646 }
647 
648 /*
649  * Change the count associated with number of processes
650  * a given user is using.  When 'max' is 0, don't enforce a limit
651  */
652 int
653 chgproccnt(struct uidinfo *uip, int diff, rlim_t max)
654 {
655     return (chglimit(uip, &uip->ui_proccnt, diff, max, "proccnt"));
656 }
657 
658 /*
659  * Change the total socket buffer size a user has used.
660  */
661 int
662 chgsbsize(struct uidinfo *uip, u_int *hiwat, u_int to, rlim_t max)
663 {
664     int diff, rv;
665 
666     diff = to - *hiwat;
667     if (diff > 0 && max == 0) {
668         rv = 0;
669     } else {
670         rv = chglimit(uip, &uip->ui_sbsize, diff, max, "sbsize");
671         if (rv != 0)
672             *hiwat = to;
673     }
674     return (rv);
675 }
676 
677 /*
678  * Change the count associated with number of pseudo-terminals
679  * a given user is using.  When 'max' is 0, don't enforce a limit
680  */
681 int
682 chgptscnt(struct uidinfo *uip, int diff, rlim_t max)
683 {
684     return (chglimit(uip, &uip->ui_ptscnt, diff, max, "ptscnt"));
685 }
686 
687 int
688 chgkqcnt(struct uidinfo *uip, int diff, rlim_t max)
689 {
690     return (chglimit(uip, &uip->ui_kqcnt, diff, max, "kqcnt"));
691 }
692 
693 int
694 chgumtxcnt(struct uidinfo *uip, int diff, rlim_t max)
695 {
696     return (chglimit(uip, &uip->ui_umtxcnt, diff, max, "umtxcnt"));
697 }
698 
699 /*
700  * Allocate a new resource limits structure and initialize its
701  * reference count and mutex pointer.
702  */
703 struct plimit *
704 lim_alloc()
705 {
706     struct plimit *limp;
707 
708     limp = malloc(sizeof(struct plimit), M_PLIMIT, M_WAITOK);
709     refcount_init(&limp->pl_refcnt, 1);
710     return (limp);
711 }
712 
713 struct plimit *
714 lim_hold(struct plimit *limp)
715 {
716     refcount_acquire(&limp->pl_refcnt);
717     return (limp);
718 }
719 
720 /*
721  * Return the current (soft) limit for a particular system resource.
722  * The which parameter which specifies the index into the rlimit array
723  */
724 rlim_t
725 lim_cur(struct thread *td, int which)
726 {
727     struct rlimit rl;
728 
729     lim_rlimit(td, which, &rl);
730     return (rl.rlim_cur);
731 }
732 
733 rlim_t
734 lim_cur_proc(struct proc *p, int which)
735 {
736     struct rlimit rl;
737 
738     lim_rlimit_proc(p, which, &rl);
739     return (rl.rlim_cur);
740 }
741 
742 /*
743  * Return a copy of the entire rlimit structure for the system limit
744  * specified by 'which' in the rlimit structure pointed to by 'rlp'.
745  */
746 void
747 lim_rlimit(struct thread *td, int which, struct rlimit *rlp)
748 {
749     struct proc *p = td->td_proc;
750 
751     MPASS(td == curthread);
752     KASSERT(which >= 0 && which < RLIM_NLIMITS,
753         ("request for invalid resource limit"));
754     *rlp = p->p_limit->pl_rlimit[which];
755     if (p->p_sysent->sv_fixlimit != NULL)
756         p->p_sysent->sv_fixlimit(rlp, which);
757 }
758 
759 void
760 lim_rlimit_proc(struct proc *p, int which, struct rlimit *rlp)
761 {
762     PROC_LOCK_ASSERT(p, MA_OWNED);
763     KASSERT(which >= 0 && which < RLIM_NLIMITS,
764         ("request for invalid resource limit"));
765     *rlp = p->p_limit->pl_rlimit[which];
766     if (p->p_sysent->sv_fixlimit != NULL)
767         p->p_sysent->sv_fixlimit(rlp, which);
768 }
769 
770 int
771 useracc(void *addr, int len, int rw)
772 {
773     return (1);
774 }
775 
776 struct pgrp *
777 pgfind(pid_t pgid)
778 {
779     return (NULL);
780 }
781 
782 struct proc *
783 zpfind(pid_t pid)
784 {
785     return (NULL);
786 }
787 
788 
789 int
790 p_cansee(struct thread *td, struct proc *p)
791 {
792     return (0);
793 }
794 
795 struct proc *
796 pfind(pid_t pid)
797 {
798     return (NULL);
799 }
800 
801 int
802 pget(pid_t pid, int flags, struct proc **pp)
803 {
804     return (ESRCH);
805 }
806 
807 struct uidinfo uid0;
808 
809 struct uidinfo *
810 uifind(uid_t uid)
811 {
812     return (&uid0);
813 }
814 
815 /*
816  * Allocate a zeroed cred structure.
817  */
818 struct ucred *
819 crget(void)
820 {
821     register struct ucred *cr;
822 
823     cr = malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO);
824     refcount_init(&cr->cr_ref, 1);
825 
826     return (cr);
827 }
828 
829 /*
830  * Claim another reference to a ucred structure.
831  */
832 struct ucred *
833 crhold(struct ucred *cr)
834 {
835     refcount_acquire(&cr->cr_ref);
836     return (cr);
837 }
838 
839 /*
840  * Free a cred structure.  Throws away space when ref count gets to 0.
841  */
842 void
843 crfree(struct ucred *cr)
844 {
845     KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
846     KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
847     if (refcount_release(&cr->cr_ref)) {
848 
849         free(cr, M_CRED);
850     }
851 }
852 
853 /*
854  * Fill in a struct xucred based on a struct ucred.
855  */
856 
857 void
858 cru2x(struct ucred *cr, struct xucred *xcr)
859 {
860 #if 0
861     int ngroups;
862 
863     bzero(xcr, sizeof(*xcr));
864     xcr->cr_version = XUCRED_VERSION;
865     xcr->cr_uid = cr->cr_uid;
866 
867     ngroups = MIN(cr->cr_ngroups, XU_NGROUPS);
868     xcr->cr_ngroups = ngroups;
869     bcopy(cr->cr_groups, xcr->cr_groups,
870         ngroups * sizeof(*cr->cr_groups));
871 #endif
872 }
873 
874 
875 int
876 cr_cansee(struct ucred *u1, struct ucred *u2)
877 {
878     return (0);
879 }
880 
881 int
882 cr_canseesocket(struct ucred *cred, struct socket *so)
883 {
884     return (0);
885 }
886 
887 int
888 cr_canseeinpcb(struct ucred *cred, struct inpcb *inp)
889 {
890     return (0);
891 }
892 
893 int
894 securelevel_gt(struct ucred *cr, int level)
895 {
896     return (0);
897 }
898 
899 int
900 securelevel_ge(struct ucred *cr, int level)
901 {
902         return (0);
903 }
904 
905 /**
906  * @brief Send a 'notification' to userland, using standard ways
907  */
908 void
909 devctl_notify(const char *system, const char *subsystem, const char *type,
910     const char *data)
911 {
912 
913 }
914 
915 void
916 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
917 {
918 
919 }
920 
921 static void
922 configure_final(void *dummy)
923 {
924     cold = 0;
925 }
926 
927 /*
928  * Send a SIGIO or SIGURG signal to a process or process group using stored
929  * credentials rather than those of the current process.
930  */
931 void
932 pgsigio(sigiop, sig, checkctty)
933     struct sigio **sigiop;
934     int sig, checkctty;
935 {
936     panic("SIGIO not supported yet\n");
937 #ifdef notyet
938     ksiginfo_t ksi;
939     struct sigio *sigio;
940 
941     ksiginfo_init(&ksi);
942     ksi.ksi_signo = sig;
943     ksi.ksi_code = SI_KERNEL;
944 
945     SIGIO_LOCK();
946     sigio = *sigiop;
947     if (sigio == NULL) {
948         SIGIO_UNLOCK();
949         return;
950     }
951     if (sigio->sio_pgid > 0) {
952         PROC_LOCK(sigio->sio_proc);
953         if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
954             psignal(sigio->sio_proc, sig);
955         PROC_UNLOCK(sigio->sio_proc);
956     } else if (sigio->sio_pgid < 0) {
957         struct proc *p;
958 
959         PGRP_LOCK(sigio->sio_pgrp);
960         LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
961             PROC_LOCK(p);
962             if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
963                 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
964                 psignal(p, sig);
965             PROC_UNLOCK(p);
966         }
967         PGRP_UNLOCK(sigio->sio_pgrp);
968     }
969     SIGIO_UNLOCK();
970 #endif
971 }
972 
973 void
974 kproc_exit(int ecode)
975 {
976     panic("kproc_exit unsupported");
977 }
978 
979 vm_offset_t
980 kmem_malloc(struct vmem *vmem, vm_size_t bytes, int flags)
981 {
982     void *alloc = ff_mmap(NULL, bytes, ff_PROT_READ|ff_PROT_WRITE, ff_MAP_ANON|ff_MAP_PRIVATE, -1, 0);
983     if ((flags & M_ZERO) && alloc != NULL)
984         bzero(alloc, bytes);
985     return ((vm_offset_t)alloc);
986 }
987 
988 void
989 kmem_free(struct vmem *vmem, vm_offset_t addr, vm_size_t size)
990 {
991     ff_munmap((void *)addr, size);
992 }
993 
994 vm_offset_t
995 kmem_alloc_contig(struct vmem *vmem, vm_size_t size, int flags, vm_paddr_t low,
996     vm_paddr_t high, u_long alignment, vm_paddr_t boundary, vm_memattr_t memattr)
997 {
998     return (kmem_malloc(vmem, size, flags));
999 }
1000 
1001 void
1002 malloc_init(void *data)
1003 {
1004     /* Nothing to do here */
1005 }
1006 
1007 
1008 void
1009 malloc_uninit(void *data)
1010 {
1011     /* Nothing to do here */
1012 }
1013 
1014 
1015 void *
1016 malloc(unsigned long size, struct malloc_type *type, int flags)
1017 {
1018     void *alloc;
1019 
1020     do {
1021         alloc = ff_malloc(size);
1022         if (alloc || !(flags & M_WAITOK))
1023             break;
1024 
1025         pause("malloc", hz/100);
1026     } while (alloc == NULL);
1027 
1028     if ((flags & M_ZERO) && alloc != NULL)
1029         bzero(alloc, size);
1030     return (alloc);
1031 }
1032 
1033 void
1034 free(void *addr, struct malloc_type *type)
1035 {
1036     ff_free(addr);
1037 }
1038 
1039 
1040 void *
1041 realloc(void *addr, unsigned long size, struct malloc_type *type,
1042     int flags)
1043 {
1044     return (ff_realloc(addr, size));
1045 }
1046 
1047 void *
1048 reallocf(void *addr, unsigned long size, struct malloc_type *type,
1049      int flags)
1050 {
1051     void *mem;
1052 
1053     if ((mem = ff_realloc(addr, size)) == NULL)
1054         ff_free(addr);
1055 
1056     return (mem);
1057 }
1058 
1059 void
1060 DELAY(int delay)
1061 {
1062     struct timespec rqt;
1063 
1064     if (delay < 1000)
1065         return;
1066 
1067     rqt.tv_nsec = 1000*((unsigned long)delay);
1068     rqt.tv_sec = 0;
1069     /*
1070      * FIXME: We shouldn't sleep in dpdk apps.
1071      */
1072     //nanosleep(&rqt, NULL);
1073 }
1074 
1075 void
1076 bwillwrite(void)
1077 {
1078 
1079 }
1080 
1081 off_t
1082 foffset_lock(struct file *fp, int flags)
1083 {
1084     struct mtx *mtxp;
1085     off_t res;
1086 
1087     KASSERT((flags & FOF_OFFSET) == 0, ("FOF_OFFSET passed"));
1088 
1089 #if OFF_MAX <= LONG_MAX
1090     /*
1091      * Caller only wants the current f_offset value.  Assume that
1092      * the long and shorter integer types reads are atomic.
1093      */
1094     if ((flags & FOF_NOLOCK) != 0)
1095         return (fp->f_offset);
1096 #endif
1097 
1098     /*
1099      * According to McKusick the vn lock was protecting f_offset here.
1100      * It is now protected by the FOFFSET_LOCKED flag.
1101      */
1102     mtxp = mtx_pool_find(mtxpool_sleep, fp);
1103     mtx_lock(mtxp);
1104     /*
1105     if ((flags & FOF_NOLOCK) == 0) {
1106         while (fp->f_vnread_flags & FOFFSET_LOCKED) {
1107             fp->f_vnread_flags |= FOFFSET_LOCK_WAITING;
1108             msleep(&fp->f_vnread_flags, mtxp, PUSER -1,
1109                 "vofflock", 0);
1110         }
1111         fp->f_vnread_flags |= FOFFSET_LOCKED;
1112     }
1113     */
1114     res = fp->f_offset;
1115     mtx_unlock(mtxp);
1116     return (res);
1117 }
1118 
1119 void
1120 sf_ext_free(void *arg1, void *arg2)
1121 {
1122     panic("sf_ext_free not implemented.\n");
1123 }
1124 
1125 void
1126 sf_ext_free_nocache(void *arg1, void *arg2)
1127 {
1128     panic("sf_ext_free_nocache not implemented.\n");
1129 }
1130 
1131 void
1132 sched_bind(struct thread *td, int cpu)
1133 {
1134 
1135 }
1136 
1137 void
1138 sched_unbind(struct thread* td)
1139 {
1140 
1141 }
1142 
1143 void
1144 getcredhostid(struct ucred *cred, unsigned long *hostid)
1145 {
1146     *hostid = 0;
1147 }
1148 
1149 /*
1150  * Check if gid is a member of the group set.
1151  */
1152 int
1153 groupmember(gid_t gid, struct ucred *cred)
1154 {
1155 	int l;
1156 	int h;
1157 	int m;
1158 
1159 	if (cred->cr_groups[0] == gid)
1160 		return(1);
1161 
1162 	/*
1163 	 * If gid was not our primary group, perform a binary search
1164 	 * of the supplemental groups.  This is possible because we
1165 	 * sort the groups in crsetgroups().
1166 	 */
1167 	l = 1;
1168 	h = cred->cr_ngroups;
1169 	while (l < h) {
1170 		m = l + ((h - l) / 2);
1171 		if (cred->cr_groups[m] < gid)
1172 			l = m + 1;
1173 		else
1174 			h = m;
1175 	}
1176 	if ((l < cred->cr_ngroups) && (cred->cr_groups[l] == gid))
1177 		return (1);
1178 
1179 	return (0);
1180 }
1181 
1182