summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorValery Ushakov <uwe@stderr.spb.ru>2017-09-10 04:12:33 +0300
committerValery Ushakov <uwe@stderr.spb.ru>2017-09-10 04:12:33 +0300
commit8eba7b79fe0fd83319cc590b9f4a6c480863fd1b (patch)
treefaea8c6463b9c99262c1dafaccc469efd6c1f952
parentce8d03217d8b7df75d799652e390ae87a7b93f8e (diff)
Switch NetBSD from clone(2) to kproc-pthreads.c
This commit is mostly a pretty mechanical change. It works on NetBSD-6 where pthread uses userland semaphores. It doesn't work on NetBSD-7 where pthread uses kernel semaphores.
-rw-r--r--NetBSD/386/include/emu.h10
-rw-r--r--emu/NetBSD/asm-386.S50
-rw-r--r--emu/NetBSD/mkfile3
-rw-r--r--emu/NetBSD/os.c238
-rw-r--r--emu/port/kproc-pthreads.c7
5 files changed, 13 insertions, 295 deletions
diff --git a/NetBSD/386/include/emu.h b/NetBSD/386/include/emu.h
index 5e03489b..07e3fad7 100644
--- a/NetBSD/386/include/emu.h
+++ b/NetBSD/386/include/emu.h
@@ -15,16 +15,8 @@ struct FPU
#define KSTACK (32 * 1024)
-static __inline Proc *getup(void) {
- Proc *p;
- __asm__( "movl %%esp, %%eax\n\t"
- : "=a" (p)
- );
- return *(Proc **)((unsigned long)p & ~(KSTACK - 1));
-};
-
+extern Proc* getup(void);
#define up (getup())
typedef sigjmp_buf osjmpbuf;
#define ossetjmp(buf) sigsetjmp(buf, 1)
-
diff --git a/emu/NetBSD/asm-386.S b/emu/NetBSD/asm-386.S
index d21c03bb..85ff3c6c 100644
--- a/emu/NetBSD/asm-386.S
+++ b/emu/NetBSD/asm-386.S
@@ -2,56 +2,6 @@
#include <sys/syscall.h>
/*
- * executeonnewstack(void *tos, void (*tramp)(void *arg), void *arg)
- */
-
- .type executeonnewstack,@function
- .global executeonnewstack
-executeonnewstack:
- pushl %ebp
- movl %esp, %ebp
- pushl %esi
-
- movl 8(%ebp), %esi /* get tos */
- subl $4, %esi
- movl 16(%ebp), %eax
- movl %eax, (%esi) /* stash arg on new stack */
- subl $4, %esi
- movl 12(%ebp), %eax
- movl %eax, (%esi) /* stash tramp on new stack */
- mov %esi, %esp /* swap stacks pronto */
- popl %eax /* recover the tramp address */
- call *%eax /* and jump to it (ho ho) */
-
- /* if we return here, tramp didn't do its job */
-
- addl $8, %esp /* clean up for pose value */
-
- leal SYS_exit, %eax
- int $0x80
-
-/*
- * unlockandexit(int *key)
- *
- * NB: the return status may be rubbish if the stack is reused
- * between the unlock and the system call, but this should
- * not matter since no task is waiting for the result
- */
-
- .type unlockandexit,@function
- .global unlockandexit
-unlockandexit:
- pushl %ebp
- movl %esp, %ebp
-
- movl 8(%ebp), %esi /* get the key address */
- pushl $0 /* exit status 0 */
- movl $0, %eax /* unlock the stack allocator */
- movl %eax, (%esi)
- leal SYS_exit, %eax /* call exit */
- int $0x80
-
-/*
* umult(ulong m1, ulong m2, ulong *hi)
*/
diff --git a/emu/NetBSD/mkfile b/emu/NetBSD/mkfile
index effbaefe..8eda46f3 100644
--- a/emu/NetBSD/mkfile
+++ b/emu/NetBSD/mkfile
@@ -24,6 +24,7 @@ X11LIBS= -L/usr/X11R7/lib -R/usr/X11R7/lib -lXext -lX11
OBJ=\
asm-$OBJTYPE.$O\
os.$O\
+ kproc-pthreads.$O\
$CONF.root.$O\
lock.$O\
$DEVS\
@@ -32,7 +33,7 @@ OBJ=\
HFILES=\
CFLAGS='-DROOT="'$ROOT'"' -DEMU -I. -I../port -I$ROOT/$SYSTARG/$OBJTYPE/include -I$ROOT/include -I$ROOT/libinterp $CTHREADFLAGS $CFLAGS $EMUOPTIONS
-SYSLIBS= ${X11LIBS} -lossaudio -lm
+SYSLIBS= ${X11LIBS} -lossaudio -lm -lpthread
KERNDATE=`{$NDATE}
default:V: $O.$CONF
diff --git a/emu/NetBSD/os.c b/emu/NetBSD/os.c
index e299a188..34526088 100644
--- a/emu/NetBSD/os.c
+++ b/emu/NetBSD/os.c
@@ -21,16 +21,9 @@ enum
{
DELETE = 0x7f,
CTRLC = 'C'-'@',
- NSTACKSPERALLOC = 16,
- X11STACK= 256*1024
};
char *hosttype = "NetBSD";
-static void *stackalloc(Proc *p, void **tos);
-static void stackfreeandexit(void *stack);
-
-void executeonnewstack(void *tos, void (*tramp)(void *arg), void *arg);
-void unlockandexit(ulong *key);
extern int dflag;
@@ -38,119 +31,6 @@ int gidnobody = -1;
int uidnobody = -1;
static struct termios tinit;
-void
-pexit(char *msg, int t)
-{
- Osenv *e;
- void *kstack;
-
- lock(&procs.l);
- if(up->prev)
- up->prev->next = up->next;
- else
- procs.head = up->next;
-
- if(up->next)
- up->next->prev = up->prev;
- else
- procs.tail = up->prev;
- unlock(&procs.l);
-
- if(0)
- print("pexit: %s: %s\n", up->text, msg);
-
- e = up->env;
- if(e != nil) {
- closefgrp(e->fgrp);
- closepgrp(e->pgrp);
- closeegrp(e->egrp);
- closesigs(e->sigs);
- }
- kstack = up->kstack;
- free(up->prog);
- free(up);
- if(kstack != nil)
- stackfreeandexit(kstack);
-}
-
-int
-tramp(void *arg)
-{
- Proc *p;
- p = arg;
- p->pid = p->sigid = getpid();
- (*p->func)(p->arg);
- pexit("{Tramp}", 0);
- return 0;
-}
-
-void
-kproc(char *name, void (*func)(void*), void *arg, int flags)
-{
- int pid;
- Proc *p;
- Pgrp *pg;
- Fgrp *fg;
- Egrp *eg;
- void *tos;
-
- p = newproc();
- if(0)
- print("start %s:%.8lx\n", name, p);
- if(p == nil) {
- print("kproc(%s): no memory", name);
- panic("kproc: no memory");
- }
-
- if(flags & KPDUPPG) {
- pg = up->env->pgrp;
- incref(&pg->r);
- p->env->pgrp = pg;
- }
- if(flags & KPDUPFDG) {
- fg = up->env->fgrp;
- incref(&fg->r);
- p->env->fgrp = fg;
- }
- if(flags & KPDUPENVG) {
- eg = up->env->egrp;
- incref(&eg->r);
- p->env->egrp = eg;
- }
-
- p->env->uid = up->env->uid;
- p->env->gid = up->env->gid;
- kstrdup(&p->env->user, up->env->user);
-
- strcpy(p->text, name);
-
- p->func = func;
- p->arg = arg;
-
- if(flags & KPX11){
- p->kstack = nil; /* never freed; also up not defined */
- tos = (char*)mallocz(X11STACK, 0) + X11STACK - sizeof(void*);
- }else
- p->kstack = stackalloc(p, &tos);
-
- lock(&procs.l);
- if(procs.tail != nil) {
- p->prev = procs.tail;
- procs.tail->next = p;
- }
- else {
- procs.head = p;
- p->prev = nil;
- }
- procs.tail = p;
- unlock(&procs.l);
-
- if (__clone(tramp, tos, /*CLONE_PTRACE|*/CLONE_VM|CLONE_FS|CLONE_FILES|SIGCHLD, p) <= 0) {
- fprint(2, "emu: clone failed: %s\n", strerror(errno));
- panic("kproc: clone failed");
- }
-}
-
/*
* TO DO:
* To get pc on trap, use sigaction instead of signal and
@@ -215,37 +95,6 @@ trapUSR1(int signo)
disfault(nil, Eintr); /* Should never happen */
}
-/* called to wake up kproc blocked on a syscall */
-void
-oshostintr(Proc *p)
-{
- kill(p->sigid, SIGUSR1);
-}
-
-static void
-trapUSR2(int signo)
-{
- USED(signo);
- /* we've done our work of interrupting sigsuspend */
-}
-
-void
-osblock(void)
-{
- sigset_t mask;
-
- sigprocmask(SIG_SETMASK, NULL, &mask);
- sigdelset(&mask, SIGUSR2);
- sigsuspend(&mask);
-}
-
-void
-osready(Proc *p)
-{
- if(kill(p->sigid, SIGUSR2) < 0)
- fprint(2, "emu: osready failed: pid %d: %s\n", p->sigid, strerror(errno));
-}
-
void
oslongjmp(void *regs, osjmpbuf env, int val)
{
@@ -301,12 +150,9 @@ osreboot(char *file, char **argv)
void
libinit(char *imod)
{
- struct termios t;
struct sigaction act;
- sigset_t mask;
struct passwd *pw;
Proc *p;
- void *tos;
char sys[64];
setsid();
@@ -323,18 +169,10 @@ libinit(char *imod)
if(dflag == 0)
termset();
- memset(&act, 0 , sizeof(act));
+ memset(&act, 0, sizeof(act));
act.sa_handler = trapUSR1;
sigaction(SIGUSR1, &act, nil);
- sigemptyset(&mask);
- sigaddset(&mask, SIGUSR2);
- sigprocmask(SIG_BLOCK, &mask, NULL);
-
- memset(&act, 0 , sizeof(act));
- act.sa_handler = trapUSR2;
- sigaction(SIGUSR2, &act, nil);
-
act.sa_handler = SIG_IGN;
sigaction(SIGCHLD, &act, nil);
@@ -360,7 +198,7 @@ libinit(char *imod)
}
p = newproc();
- p->kstack = stackalloc(p, &tos);
+ kprocinit(p);
pw = getpwuid(getuid());
if(pw != nil)
@@ -371,7 +209,7 @@ libinit(char *imod)
p->env->uid = getuid();
p->env->gid = getgid();
- executeonnewstack(tos, emuinit, imod);
+ emuinit(imod);
}
int
@@ -458,76 +296,6 @@ limbosleep(ulong milsec)
return osmillisleep(milsec);
}
-void
-osyield(void)
-{
- sched_yield();
-}
-
-void
-ospause(void)
-{
- for(;;)
- pause();
-}
-
-void
-oslopri(void)
-{
- setpriority(PRIO_PROCESS, 0, getpriority(PRIO_PROCESS,0)+4);
-}
-
-static struct {
- Lock l;
- void *free;
-} stacklist;
-
-static void
-_stackfree(void *stack)
-{
- *((void **)stack) = stacklist.free;
- stacklist.free = stack;
-}
-
-static void
-stackfreeandexit(void *stack)
-{
- lock(&stacklist.l);
- _stackfree(stack);
- unlockandexit(&stacklist.l.val);
-}
-
-static void *
-stackalloc(Proc *p, void **tos)
-{
- void *rv;
- lock(&stacklist.l);
- if (stacklist.free == 0) {
- int x;
- /*
- * obtain some more by using sbrk()
- */
- void *more = sbrk(KSTACK * (NSTACKSPERALLOC + 1));
- if (more == 0)
- panic("stackalloc: no more stacks");
- /*
- * align to KSTACK
- */
- more = (void *)((((unsigned long)more) + (KSTACK - 1)) & ~(KSTACK - 1));
- /*
- * free all the new stacks onto the freelist
- */
- for (x = 0; x < NSTACKSPERALLOC; x++)
- _stackfree((char *)more + KSTACK * x);
- }
- rv = stacklist.free;
- stacklist.free = *(void **)rv;
- unlock(&stacklist.l);
- *tos = rv + KSTACK - sizeof(void *);
- *(Proc **)rv = p;
- return rv;
-}
-
int
segflush(void *a, ulong n)
{
diff --git a/emu/port/kproc-pthreads.c b/emu/port/kproc-pthreads.c
index 32053794..a15a7cd3 100644
--- a/emu/port/kproc-pthreads.c
+++ b/emu/port/kproc-pthreads.c
@@ -12,6 +12,13 @@
#include <errno.h>
#include <semaphore.h>
+#ifdef __NetBSD__
+#include <sched.h>
+#define pthread_yield() (sched_yield())
+#define PTHREAD_STACK_MIN ((size_t)sysconf(_SC_THREAD_STACK_MIN))
+#endif
+
+
typedef struct Osdep Osdep;
struct Osdep {
sem_t sem;