From bff3f981d206d1b2f6be49172fbf7a13f358db19 Mon Sep 17 00:00:00 2001
From: Chris Johns <chrisj@rtems.org>
Date: Wed, 21 Jul 2021 13:46:45 +1000
Subject: [PATCH rtems-libbsd v4 10/19] kern: Add a proc0

- Provides the thread's proc pointer and with that access to creds

Update #4475
---
 freebsd/sys/kern/init_main.c         |  4 +++
 freebsd/sys/kern/kern_prot.c         | 45 ++++++++++++++++++++++--
 freebsd/sys/kern/kern_resource.c     | 12 +++++++
 freebsd/sys/sys/proc.h               | 51 +++++++++++++++++++++++++---
 freebsd/sys/sys/ucred.h              | 21 +++++-------
 libbsd.py                            |  2 ++
 rtemsbsd/rtems/rtems-kernel-init.c   | 38 +++++++++++++++++++++
 rtemsbsd/rtems/rtems-kernel-thread.c | 25 +++++++++-----
 8 files changed, 168 insertions(+), 30 deletions(-)

diff --git a/freebsd/sys/kern/init_main.c b/freebsd/sys/kern/init_main.c
index 258fa8cd..df7e514c 100644
--- a/freebsd/sys/kern/init_main.c
+++ b/freebsd/sys/kern/init_main.c
@@ -103,7 +103,9 @@ void mi_startup(void);				/* Should be elsewhere */
 /* Components of the first process -- never freed. */
 static struct session session0;
 static struct pgrp pgrp0;
+#endif /* __rtems__ */
 struct	proc proc0;
+#ifndef __rtems__
 struct thread0_storage thread0_st __aligned(32);
 struct	vmspace vmspace0;
 struct	proc *initproc;
@@ -121,6 +123,7 @@ SYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD, &boothowto, 0,
 int	bootverbose = BOOTVERBOSE;
 SYSCTL_INT(_debug, OID_AUTO, bootverbose, CTLFLAG_RW, &bootverbose, 0,
 	"Control the output of verbose kernel messages");
+#endif /* __rtems__ */
 
 #ifdef VERBOSE_SYSINIT
 /*
@@ -134,6 +137,7 @@ int	verbose_sysinit = VERBOSE_SYSINIT;
 TUNABLE_INT("debug.verbose_sysinit", &verbose_sysinit);
 #endif
 
+#ifndef __rtems__
 #ifdef INVARIANTS
 FEATURE(invariants, "Kernel compiled with INVARIANTS, may affect performance");
 #endif
diff --git a/freebsd/sys/kern/kern_prot.c b/freebsd/sys/kern/kern_prot.c
index a62b6f76..0df18a12 100644
--- a/freebsd/sys/kern/kern_prot.c
+++ b/freebsd/sys/kern/kern_prot.c
@@ -1,3 +1,5 @@
+#include <machine/rtems-bsd-kernel-space.h>
+
 /*-
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -46,12 +48,14 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
-#include "opt_inet.h"
-#include "opt_inet6.h"
+#include <rtems/bsd/local/opt_inet.h>
+#include <rtems/bsd/local/opt_inet6.h>
 
 #include <sys/param.h>
 #include <sys/systm.h>
+#ifndef __rtems__
 #include <sys/acct.h>
+#endif /* __rtems__ */
 #include <sys/kdb.h>
 #include <sys/kernel.h>
 #include <sys/lock.h>
@@ -65,13 +69,19 @@ __FBSDID("$FreeBSD$");
 #include <sys/sysent.h>
 #include <sys/sysproto.h>
 #include <sys/jail.h>
+#ifndef __rtems__
 #include <sys/pioctl.h>
+#endif /* __rtems__ */
 #include <sys/racct.h>
+#ifndef __rtems__
 #include <sys/rctl.h>
+#endif /* __rtems__ */
 #include <sys/resourcevar.h>
 #include <sys/socket.h>
 #include <sys/socketvar.h>
+#ifndef __rtems__
 #include <sys/syscallsubr.h>
+#endif /* __rtems__ */
 #include <sys/sysctl.h>
 
 #ifdef REGRESSION
@@ -79,11 +89,14 @@ FEATURE(regression,
     "Kernel support for interfaces necessary for regression testing (SECURITY RISK!)");
 #endif
 
+#ifndef __rtems__
 #include <security/audit/audit.h>
 #include <security/mac/mac_framework.h>
+#endif /* __rtems__ */
 
 static MALLOC_DEFINE(M_CRED, "cred", "credentials");
 
+#ifndef __rtems__
 SYSCTL_NODE(_security, OID_AUTO, bsd, CTLFLAG_RW, 0, "BSD security policy");
 
 static void crsetgroups_locked(struct ucred *cr, int ngrp,
@@ -1260,6 +1273,7 @@ sys___setugid(struct thread *td, struct __setugid_args *uap)
 	return (ENOSYS);
 #endif /* REGRESSION */
 }
+#endif /* __rtems__ */
 
 /*
  * Check if gid is a member of the group set.
@@ -1294,6 +1308,7 @@ groupmember(gid_t gid, struct ucred *cred)
 	return (0);
 }
 
+#ifndef __rtems__
 /*
  * Test the active securelevel against a given level.  securelevel_gt()
  * implements (securelevel > level).  securelevel_ge() implements
@@ -1807,6 +1822,7 @@ p_canwait(struct thread *td, struct proc *p)
 
 	return (0);
 }
+#endif /* __rtems__ */
 
 /*
  * Allocate a zeroed cred structure.
@@ -1836,7 +1852,10 @@ crget(void)
 struct ucred *
 crhold(struct ucred *cr)
 {
-
+#ifdef __rtems__
+	if (cr == NULL)
+		return (cr);
+#endif /* __rtems__ */
 	refcount_acquire(&cr->cr_ref);
 	return (cr);
 }
@@ -1847,6 +1866,10 @@ crhold(struct ucred *cr)
 void
 crfree(struct ucred *cr)
 {
+#ifdef __rtems__
+	if (cr == NULL)
+		return;
+#endif /* __rtems__ */
 
 	KASSERT(cr->cr_ref > 0, ("bad ucred refcount: %d", cr->cr_ref));
 	KASSERT(cr->cr_ref != 0xdeadc0de, ("dangling reference to ucred"));
@@ -1863,6 +1886,7 @@ crfree(struct ucred *cr)
 		/*
 		 * Free a prison, if any.
 		 */
+#ifndef __rtems__
 		if (cr->cr_prison != NULL)
 			prison_free(cr->cr_prison);
 		if (cr->cr_loginclass != NULL)
@@ -1873,6 +1897,7 @@ crfree(struct ucred *cr)
 #ifdef MAC
 		mac_cred_destroy(cr);
 #endif
+#endif /* __rtems__ */
 		if (cr->cr_groups != cr->cr_smallgroups)
 			free(cr->cr_groups, M_CRED);
 		free(cr, M_CRED);
@@ -1885,6 +1910,10 @@ crfree(struct ucred *cr)
 void
 crcopy(struct ucred *dest, struct ucred *src)
 {
+#ifdef __rtems__
+	if (dest == NULL || src == NULL)
+		return;
+#endif /* __rtems__ */
 
 	KASSERT(dest->cr_ref == 1, ("crcopy of shared ucred"));
 	bcopy(&src->cr_startcopy, &dest->cr_startcopy,
@@ -1893,6 +1922,7 @@ crcopy(struct ucred *dest, struct ucred *src)
 	crsetgroups(dest, src->cr_ngroups, src->cr_groups);
 	uihold(dest->cr_uidinfo);
 	uihold(dest->cr_ruidinfo);
+#ifndef __rtems__
 	prison_hold(dest->cr_prison);
 	loginclass_hold(dest->cr_loginclass);
 #ifdef AUDIT
@@ -1901,6 +1931,7 @@ crcopy(struct ucred *dest, struct ucred *src)
 #ifdef MAC
 	mac_cred_copy(src, dest);
 #endif
+#endif /* __rtems__ */
 }
 
 /*
@@ -1911,6 +1942,10 @@ crdup(struct ucred *cr)
 {
 	struct ucred *newcr;
 
+#ifdef __rtems__
+	if (cr == NULL)
+		return NULL;
+#endif /* __rtems__ */
 	newcr = crget();
 	crcopy(newcr, cr);
 	return (newcr);
@@ -1934,6 +1969,7 @@ cru2x(struct ucred *cr, struct xucred *xcr)
 	    ngroups * sizeof(*cr->cr_groups));
 }
 
+#ifndef __rtems__
 /*
  * Set initial process credentials.
  * Callers are responsible for providing the reference for provided credentials.
@@ -1993,6 +2029,7 @@ crcopysafe(struct proc *p, struct ucred *cr)
 
 	return (oldcred);
 }
+#endif /* __rtems__ */
 
 /*
  * Extend the passed in credential to hold n items.
@@ -2083,6 +2120,7 @@ crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
 	crsetgroups_locked(cr, ngrp, groups);
 }
 
+#ifndef __rtems__
 /*
  * Get login name, if available.
  */
@@ -2157,6 +2195,7 @@ setsugid(struct proc *p)
 	if (!(p->p_pfsflags & PF_ISUGID))
 		p->p_stops = 0;
 }
+#endif /* __rtems__ */
 
 /*-
  * Change a process's effective uid.
diff --git a/freebsd/sys/kern/kern_resource.c b/freebsd/sys/kern/kern_resource.c
index 49b1e0fe..2c6fb652 100644
--- a/freebsd/sys/kern/kern_resource.c
+++ b/freebsd/sys/kern/kern_resource.c
@@ -1,3 +1,5 @@
+#include <machine/rtems-bsd-kernel-space.h>
+
 /*-
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -59,15 +61,21 @@ __FBSDID("$FreeBSD$");
 #include <sys/sysctl.h>
 #include <sys/sysent.h>
 #include <sys/time.h>
+#ifndef __rtems__
 #include <sys/umtx.h>
+#endif /* __rtems__ */
 
+#ifndef __rtems__
 #include <vm/vm.h>
 #include <vm/vm_param.h>
 #include <vm/pmap.h>
 #include <vm/vm_map.h>
+#endif /* __rtems__ */
 
 
+#ifndef __rtems__
 static MALLOC_DEFINE(M_PLIMIT, "plimit", "plimit structures");
+#endif /* __rtems__ */
 static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures");
 #define	UIHASH(uid)	(&uihashtbl[(uid) & uihash])
 static struct rwlock uihashtbl_lock;
@@ -80,6 +88,7 @@ static int	donice(struct thread *td, struct proc *chgp, int n);
 static struct uidinfo *uilookup(uid_t uid);
 static void	ruxagg_locked(struct rusage_ext *rux, struct thread *td);
 
+#ifndef __rtems__
 /*
  * Resource controls and accounting.
  */
@@ -1303,6 +1312,7 @@ lim_rlimit_proc(struct proc *p, int which, struct rlimit *rlp)
 	if (p->p_sysent->sv_fixlimit != NULL)
 		p->p_sysent->sv_fixlimit(rlp, which);
 }
+#endif /* __rtems__ */
 
 void
 uihashinit()
@@ -1443,6 +1453,7 @@ uifree(struct uidinfo *uip)
 	free(uip, M_UIDINFO);
 }
 
+#ifndef __rtems__
 #ifdef RACCT
 void
 ui_racct_foreach(void (*callback)(struct racct *racct,
@@ -1537,3 +1548,4 @@ chgumtxcnt(struct uidinfo *uip, int diff, rlim_t max)
 
 	return (chglimit(uip, &uip->ui_umtxcnt, diff, max, "umtxcnt"));
 }
+#endif /* __rtems__ */
diff --git a/freebsd/sys/sys/proc.h b/freebsd/sys/sys/proc.h
index 04a0e430..bb9e91ce 100644
--- a/freebsd/sys/sys/proc.h
+++ b/freebsd/sys/sys/proc.h
@@ -74,6 +74,7 @@
 #include <machine/cpu.h>
 #endif
 #ifdef __rtems__
+#include <rtems/score/thread.h>
 #include <sys/epoch.h>
 #endif /* __rtems__ */
 
@@ -243,7 +244,9 @@ struct thread {
 #endif /* __rtems__ */
 #ifndef __rtems__
 	struct mtx	*volatile td_lock; /* replaces sched lock */
+#endif /* __rtems__ */
 	struct proc	*td_proc;	/* (*) Associated process. */
+#ifndef __rtems__
 	TAILQ_ENTRY(thread) td_plist;	/* (*) All threads in this proc. */
 	TAILQ_ENTRY(thread) td_runq;	/* (t) Run queue. */
 #endif /* __rtems__ */
@@ -261,6 +264,10 @@ struct thread {
 	struct rl_q_entry *td_rlqe;	/* (k) Associated range lock entry. */
 	struct umtx_q   *td_umtxq;	/* (c?) Link for when we're blocked. */
 	lwpid_t		td_tid;		/* (b) Thread ID. */
+#else /* __rtems__ */
+        #define td_tid td_thread->Object.id
+#endif /* __rtems__ */
+#ifndef __rtems__
 	sigqueue_t	td_sigqueue;	/* (c) Sigs arrived, not delivered. */
 #define	td_siglist	td_sigqueue.sq_signals
 	u_char		td_lend_user_pri; /* (t) Lend user pri. */
@@ -270,7 +277,9 @@ struct thread {
 	u_char		td_epochnest;	/* (k) Epoch nest counter. */
 	int		td_flags;	/* (t) TDF_* flags. */
 	int		td_inhibitors;	/* (t) Why can not run. */
+#endif /* __rtems__ */
 	int		td_pflags;	/* (k) Private thread (TDP_*) flags. */
+#ifndef __rtems__
 	int		td_dupfd;	/* (k) Ret value from fdopen. XXX */
 #endif /* __rtems__ */
 #ifdef __rtems__
@@ -285,7 +294,9 @@ struct thread {
 	short		td_locks;	/* (k) Debug: count of non-spin locks */
 	short		td_rw_rlocks;	/* (k) Count of rwlock read locks. */
 	short		td_sx_slocks;	/* (k) Count of sx shared locks. */
+#endif /* __rtems__ */
 	short		td_lk_slocks;	/* (k) Count of lockmgr shared locks. */
+#ifndef __rtems__
 	short		td_stopsched;	/* (k) Scheduler stopped. */
 	struct turnstile *td_blocked;	/* (t) Lock thread is blocked on. */
 	const char	*td_lockname;	/* (t) Name of lock blocked on. */
@@ -319,6 +330,10 @@ struct thread {
 	u_long		td_profil_addr;	/* (k) Temporary addr until AST. */
 	u_int		td_profil_ticks; /* (k) Temporary ticks until AST. */
 	char		td_name[MAXCOMLEN + 1];	/* (*) Thread name. */
+#else /* __rtems__ */
+#if KTR
+	char		td_name[MAXCOMLEN + 1];	/* (*) Thread name. */
+#endif
 #endif /* __rtems__ */
 	struct file	*td_fpop;	/* (k) file referencing cdev under op */
 #ifndef __rtems__
@@ -328,7 +343,9 @@ struct thread {
 	struct osd	td_osd;		/* (k) Object specific data. */
 	struct vm_map_entry *td_map_def_user; /* (k) Deferred entries. */
 	pid_t		td_dbg_forked;	/* (c) Child pid for debugger. */
+#endif /* __rtems__ */
 	u_int		td_vp_reserv;	/* (k) Count of reserved vnodes. */
+#ifndef __rtems__
 	int		td_no_sleeping;	/* (k) Sleeping disabled count. */
 	void		*td_su;		/* (k) FFS SU private */
 	sbintime_t	td_sleeptimo;	/* (t) Sleep timeout. */
@@ -365,15 +382,13 @@ struct thread {
 		TDS_RUNQ,
 		TDS_RUNNING
 	} td_state;			/* (t) thread state */
+#endif /* __rtems__ */
 	union {
 		register_t	tdu_retval[2];
 		off_t		tdu_off;
 	} td_uretoff;			/* (k) Syscall aux returns. */
-#else /* __rtems__ */
-	register_t	td_retval[2];	/* (k) Syscall aux returns. */
-#endif /* __rtems__ */
-#ifndef __rtems__
 #define td_retval	td_uretoff.tdu_retval
+#ifndef __rtems__
 	u_int		td_cowgen;	/* (k) Generation of COW pointers. */
 	/* LP64 hole */
 	struct callout	td_slpcallout;	/* (h) Callout for sleep. */
@@ -615,9 +630,11 @@ struct proc {
 	LIST_ENTRY(proc) p_list;	/* (d) List of all processes. */
 	TAILQ_HEAD(, thread) p_threads;	/* (c) all threads. */
 	struct mtx	p_slock;	/* process spin lock */
+#endif /* __rtems__ */
 	struct ucred	*p_ucred;	/* (c) Process owner's identity. */
 	struct filedesc	*p_fd;		/* (b) Open files. */
 	struct filedesc_to_leader *p_fdtol; /* (b) Tracking node */
+#ifndef __rtems__
 	struct pstats	*p_stats;	/* (b) Accounting/statistics (CPU). */
 	struct plimit	*p_limit;	/* (c) Resource limits. */
 	struct callout	p_limco;	/* (c) Limit callout handle */
@@ -630,7 +647,9 @@ struct proc {
 		PRS_NORMAL,		/* threads can be run. */
 		PRS_ZOMBIE
 	} p_state;			/* (j/c) Process status. */
+#endif /* __rtems__ */
 	pid_t		p_pid;		/* (b) Process identifier. */
+#ifndef __rtems__
 	LIST_ENTRY(proc) p_hash;	/* (d) Hash chain. */
 	LIST_ENTRY(proc) p_pglist;	/* (g + e) List of processes in pgrp. */
 	struct proc	*p_pptr;	/* (c + e) Pointer to parent process. */
@@ -675,7 +694,9 @@ struct proc {
 	char		p_step;		/* (c) Process is stopped. */
 	u_char		p_pfsflags;	/* (c) Procfs flags. */
 	u_int		p_ptevents;	/* (c + e) ptrace() event mask. */
+#endif /* __rtems__ */
 	struct nlminfo	*p_nlminfo;	/* (?) Only used by/for lockd. */
+#ifndef __rtems__
 	struct kaioinfo	*p_aioinfo;	/* (y) ASYNC I/O info. */
 	struct thread	*p_singlethread;/* (c + j) If single threading this is it */
 	int		p_suspcount;	/* (j) Num threads in suspended mode. */
@@ -887,6 +908,7 @@ MALLOC_DECLARE(M_SUBPROC);
 #define	NO_PID		100000
 extern pid_t pid_max;
 
+#ifndef __rtems__
 #define	SESS_LEADER(p)	((p)->p_session->s_leader == (p))
 
 
@@ -949,7 +971,6 @@ extern pid_t pid_max;
  * _PHOLD(), it only guarantees that exit1() is not executed,
  * faultin() is not called.
  */
-#ifndef __rtems__
 #define	PHOLD(p) do {							\
 	PROC_LOCK(p);							\
 	_PHOLD(p);							\
@@ -994,8 +1015,28 @@ extern pid_t pid_max;
 	(p)->p_cowgen++;						\
 } while (0)
 #else /* __rtems__ */
+#define SESS_LEADER(p) (1)
+#define STOPEVENT(p, e, v) do { } while (0)
+#define PROC_LOCK(p) do { } while (0)
+#define PROC_TRYLOCK(p) do { } while (0)
+#define	PROC_UNLOCK(p) do { } while (0)
+#define	PROC_LOCKED(p) do { } while (0)
+#define	PROC_LOCK_ASSERT(p, type) do { } while (0)
+#define	PGRP_LOCK(pg) do { } while (0)
+#define	PGRP_UNLOCK(pg) do { } while (0)
+#define	PGRP_LOCKED(pg) do { } while (0)
+#define	PGRP_LOCK_ASSERT(pg, type) do { } while (0)
+#define	PGRP_LOCK_PGSIGNAL(pg) do { } while (0)
+#define	PGRP_UNLOCK_PGSIGNAL(pg) do { } while (0)
+#define	SESS_LOCK(s) do { } while (0)
+#define	SESS_UNLOCK(s) do { } while (0)
+#define	SESS_LOCKED(s) do { } while (0)
+#define	SESS_LOCK_ASSERT(s, type) do { } while (0)
 #define	PHOLD(x) do { } while (0)
+#define	_PHOLD(x) do { } while (0)
+#define PROC_ASSERT_HELD(p) do { } while (0)
 #define	PRELE(x) do { } while (0)
+#define PROC_ASSERT_NOT_HELD(p) do { } while (0)
 #endif /* __rtems__ */
 
 /* Check whether a thread is safe to be swapped out. */
diff --git a/freebsd/sys/sys/ucred.h b/freebsd/sys/sys/ucred.h
index 37a93357..bc40a30b 100644
--- a/freebsd/sys/sys/ucred.h
+++ b/freebsd/sys/sys/ucred.h
@@ -48,7 +48,6 @@ struct loginclass;
  * priv(9) interface should be used to check for privilege.
  */
 #if defined(_KERNEL) || defined(_WANT_UCRED)
-#ifndef __rtems__
 struct ucred {
 	u_int	cr_ref;			/* reference count */
 #define	cr_startcopy cr_uid
@@ -60,20 +59,21 @@ struct ucred {
 	gid_t	cr_svgid;		/* saved group id */
 	struct uidinfo	*cr_uidinfo;	/* per euid resource consumption */
 	struct uidinfo	*cr_ruidinfo;	/* per ruid resource consumption */
+#ifndef __rtems__
 	struct prison	*cr_prison;	/* jail(2) */
 	struct loginclass	*cr_loginclass; /* login class */
+#endif /* __rtems__ */
 	u_int		cr_flags;	/* credential flags */
 	void 		*cr_pspare2[2];	/* general use 2 */
 #define	cr_endcopy	cr_label
 	struct label	*cr_label;	/* MAC label */
+#ifndef __rtems__
 	struct auditinfo_addr	cr_audit;	/* Audit properties. */
+#endif /* __rtems__ */
 	gid_t	*cr_groups;		/* groups */
 	int	cr_agroups;		/* Available groups */
 	gid_t   cr_smallgroups[XU_NGROUPS];	/* storage for small groups */
 };
-#else /* __rtems__ */
-struct ucred;
-#endif /* __rtems__ */
 #define	NOCRED	((struct ucred *)0)	/* no credential available */
 #define	FSCRED	((struct ucred *)-1)	/* filesystem credential */
 #endif /* _KERNEL || _WANT_UCRED */
@@ -87,13 +87,11 @@ struct ucred;
  * This is the external representation of struct ucred.
  */
 struct xucred {
-#ifndef __rtems__
 	u_int	cr_version;		/* structure layout version */
 	uid_t	cr_uid;			/* effective user id */
 	short	cr_ngroups;		/* number of groups */
 	gid_t	cr_groups[XU_NGROUPS];	/* groups */
 	void	*_cr_unused1;		/* compatibility with old ucred */
-#endif /* __rtems__ */
 };
 #define	XUCRED_VERSION	0
 
@@ -104,7 +102,6 @@ struct xucred {
 struct proc;
 struct thread;
 
-#ifndef __rtems__
 void	change_egid(struct ucred *newcred, gid_t egid);
 void	change_euid(struct ucred *newcred, struct uidinfo *euip);
 void	change_rgid(struct ucred *newcred, gid_t rgid);
@@ -115,20 +112,18 @@ void	crcopy(struct ucred *dest, struct ucred *src);
 struct ucred	*crcopysafe(struct proc *p, struct ucred *cr);
 struct ucred	*crdup(struct ucred *cr);
 void	crextend(struct ucred *cr, int n);
+#ifndef __rtems__
 void	proc_set_cred_init(struct proc *p, struct ucred *cr);
 struct ucred	*proc_set_cred(struct proc *p, struct ucred *cr);
+#endif /* __rtems__ */
 void	crfree(struct ucred *cr);
 struct ucred	*crget(void);
 struct ucred	*crhold(struct ucred *cr);
+#ifndef __rtems__
 void	cru2x(struct ucred *cr, struct xucred *xcr);
+#endif /* __rtems__ */
 void	crsetgroups(struct ucred *cr, int n, gid_t *groups);
 int	groupmember(gid_t gid, struct ucred *cred);
-#else /* __rtems__ */
-#define crfree(cr) do { } while (0)
-#define crhold(cr) NULL
-#define cru2x(cr, xcr) do { } while (0)
-#define groupmember(gid, cred) 1
-#endif /* __rtems__ */
 #endif /* _KERNEL */
 
 #endif /* !_SYS_UCRED_H_ */
diff --git a/libbsd.py b/libbsd.py
index 2318e470..e6d9bc00 100644
--- a/libbsd.py
+++ b/libbsd.py
@@ -504,6 +504,8 @@ class base(builder.Module):
                 'sys/kern/kern_module.c',
                 'sys/kern/kern_mtxpool.c',
                 'sys/kern/kern_osd.c',
+                'sys/kern/kern_prot.c',
+                'sys/kern/kern_resource.c',
                 'sys/kern/kern_synch.c',
                 'sys/kern/kern_sysctl.c',
                 'sys/kern/kern_time.c',
diff --git a/rtemsbsd/rtems/rtems-kernel-init.c b/rtemsbsd/rtems/rtems-kernel-init.c
index 7112914e..f76e7cd7 100644
--- a/rtemsbsd/rtems/rtems-kernel-init.c
+++ b/rtemsbsd/rtems/rtems-kernel-init.c
@@ -50,6 +50,9 @@
 #include <sys/proc.h>
 #include <sys/stat.h>
 #include <sys/kbio.h>
+#include <sys/resourcevar.h>
+#include <sys/jail.h>
+#include <uuid/uuid.h>
 
 #include <limits.h>
 
@@ -94,6 +97,7 @@ sbintime_t sbt_timethreshold;
 sbintime_t sbt_tickthreshold;
 struct bintime tc_tick_bt;
 sbintime_t tc_tick_sbt;
+int maxproc;
 int tc_precexp;
 
 static SYSCTL_NODE(_kern, OID_AUTO, smp, CTLFLAG_RD|CTLFLAG_CAPRD, NULL,
@@ -107,6 +111,38 @@ SYSCTL_INT(_kern_smp, OID_AUTO, maxid, CTLFLAG_RD|CTLFLAG_CAPRD,
 SYSCTL_INT(_kern_smp, OID_AUTO, maxcpus, CTLFLAG_RD|CTLFLAG_CAPRD,
     &maxid_maxcpus, 0, "Max number of CPUs that the system was compiled for.");
 
+/*
+ * Create a single process. RTEMS is a single address, single process OS.
+ */
+static void
+proc0_init(void* dummy)
+{
+	struct proc *p = &proc0;
+	struct ucred *newcred;
+	struct uidinfo tmpuinfo;
+	uuid_t uuid;
+	uihashinit();
+	/* Create the file descriptor table. */
+	newcred = crget();
+	newcred->cr_uid = 0;
+	newcred->cr_ruid = 0;
+	newcred->cr_ngroups = 1;	/* group 0 */
+	newcred->cr_groups[0] = 0;
+	newcred->cr_rgid = 0;
+	tmpuinfo.ui_uid = 1;
+	curthread->td_ucred = newcred;
+	newcred->cr_uidinfo = newcred->cr_ruidinfo = &tmpuinfo;
+	newcred->cr_uidinfo = uifind(0);
+	newcred->cr_ruidinfo = uifind(0);
+	p->p_ucred = newcred;
+	p->p_pid = getpid();
+	p->p_fd = NULL;
+	p->p_fdtol = NULL;
+	uuid_generate(uuid);
+	uuid_unparse(uuid, prison0.pr_hostuuid);
+}
+SYSINIT(p0init, SI_SUB_INTRINSIC, SI_ORDER_FIRST, proc0_init, NULL);
+
 rtems_status_code
 rtems_bsd_initialize(void)
 {
@@ -135,6 +171,8 @@ rtems_bsd_initialize(void)
 	sbt_tickthreshold = bttosbt(bt_tickthreshold);
 	maxid_maxcpus = (int) rtems_scheduler_get_processor_maximum();
 
+	maxproc = 16;
+
 	mkdir("/etc", S_IRWXU | S_IRWXG | S_IRWXO);
 
 	sc =  rtems_timer_initiate_server(
diff --git a/rtemsbsd/rtems/rtems-kernel-thread.c b/rtemsbsd/rtems/rtems-kernel-thread.c
index 8e3344ef..3e1e44b9 100644
--- a/rtemsbsd/rtems/rtems-kernel-thread.c
+++ b/rtemsbsd/rtems/rtems-kernel-thread.c
@@ -90,15 +90,19 @@ struct thread *
 rtems_bsd_thread_create(Thread_Control *thread, int wait)
 {
 	struct thread *td = malloc(sizeof(*td), M_TEMP, M_ZERO | wait);
-	struct sleepqueue *sq = sleepq_alloc();
 
-	if (td != NULL && sq != NULL) {
-		td->td_thread = thread;
-		td->td_sleepqueue = sq;
-	} else {
-		free(td, M_TEMP);
-		sleepq_free(sq);
-		td = NULL;
+	if (td != NULL) {
+		struct sleepqueue *sq = sleepq_alloc();
+		if (sq != NULL) {
+			td->td_proc = &proc0;
+			td->td_ucred = proc0.p_ucred;
+			td->td_thread = thread;
+			td->td_sleepqueue = sq;
+			crhold(td->td_ucred);
+		} else {
+			free(td, M_TEMP);
+			td = NULL;
+		}
 	}
 
 	thread->extensions[rtems_bsd_extension_index] = td;
@@ -167,6 +171,7 @@ rtems_bsd_extension_thread_delete(
 	if (td != NULL) {
 		seltdfini(td);
 		sleepq_free(td->td_sleepqueue);
+		crfree(td->td_ucred);
 		free(td, M_TEMP);
 	}
 }
@@ -303,7 +308,7 @@ kproc_create(void (*func)(void *), void *arg, struct proc **newpp, int flags, in
 	va_list ap;
 
 	va_start(ap, fmt);
-	eno = rtems_bsd_thread_start(newpp, func, arg, flags, pages, fmt, ap);
+	eno = rtems_bsd_thread_start((struct thread**) newpp, func, arg, flags, pages, fmt, ap);
 	va_end(ap);
 
 	return eno;
@@ -331,6 +336,8 @@ kthread_add(void (*func)(void *), void *arg, struct proc *p, struct thread **new
 	va_list ap;
 
 	va_start(ap, fmt);
+	/* the cast here is a hack but passing a proc as a thread struct is just wrong and I
+	 * have no idea why it is like this */
 	eno = rtems_bsd_thread_start(newtdp, func, arg, flags, pages, fmt, ap);
 	va_end(ap);
 
-- 
2.24.1

