git: 9front

Download patch

ref: 9b66ea61bc5963549af5d572f8dda128bf813164
parent: fc9e8b2b11001c3f90fbf6cefb0ec612c754da2c
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Tue Nov 25 20:14:53 EST 2025

kernel: make execregs() arguments more obvious

Just pass in argc, argv and tos into execregs()
from sysexec().

Check for argc overflow in sysexec(): (must be positive).

Remove "sysprocsetup()" cruft from arm.

Use proper Ureg* type for dbgreg and noteureg.

--- a/sys/src/9/arm64/trap.c
+++ b/sys/src/9/arm64/trap.c
@@ -340,8 +340,7 @@
 uintptr
 userpc(void)
 {
-	Ureg *ur = up->dbgreg;
-	return ur->pc;
+	return up->dbgreg->pc;
 }
 
 uintptr
@@ -407,19 +406,19 @@
 }
 
 uintptr
-execregs(uintptr entry, ulong ssize, ulong nargs)
+execregs(uintptr entry, int argc, char *argv[], Tos *tos)
 {
-	uintptr *sp;
+	uintptr *sp = (void*)argv;
 	Ureg *ureg;
 
-	sp = (uintptr*)(USTKTOP - ssize);
-	*--sp = nargs;
+	*--sp = argc;
 
 	ureg = up->dbgreg;
 	ureg->sp = (uintptr)sp;
 	ureg->pc = entry;
 	ureg->link = 0;
-	return USTKTOP-sizeof(Tos);
+
+	return (uintptr)tos;
 }
 
 void
--- a/sys/src/9/bcm/arch.c
+++ b/sys/src/9/bcm/arch.c
@@ -92,7 +92,7 @@
 void
 procsetup(Proc* p)
 {
-	fpusysprocsetup(p);
+	fpuprocsetup(p);
 }
 
 void
--- a/sys/src/9/bcm/fns.h
+++ b/sys/src/9/bcm/fns.h
@@ -114,7 +114,7 @@
 extern void fpuprocrestore(Proc*);
 extern void fpuprocsave(Proc*);
 extern void fpuprocfork(Proc*);
-extern void fpusysprocsetup(Proc*);
+extern void fpuprocsetup(Proc*);
 extern int fpuemu(Ureg*);
 
 /*
@@ -126,8 +126,6 @@
 extern void evenaddr(uintptr);
 extern void idlehands(void);
 extern void setkernur(Ureg*, Proc*);		/* only devproc.c */
-extern void* sysexecregs(uintptr, ulong, int);
-extern void sysprocsetup(Proc*);
 
 #define	getpgcolor(a)	0
 #define	kmapinval()
--- a/sys/src/9/bcm/vfp3.c
+++ b/sys/src/9/bcm/vfp3.c
@@ -379,11 +379,11 @@
 
 /*
  * Disable the FPU.
- * Called from sysexec() via sysprocsetup() to
+ * Called from sysexec() via procsetup() to
  * set the FPU for the new process.
  */
 void
-fpusysprocsetup(Proc *p)
+fpuprocsetup(Proc *p)
 {
 	int s;
 
--- a/sys/src/9/cycv/trap.c
+++ b/sys/src/9/cycv/trap.c
@@ -337,10 +337,7 @@
 uintptr
 userpc(void)
 {
-	Ureg *ur;
-	
-	ur = up->dbgreg;
-	return ur->pc;
+	return up->dbgreg->pc;
 }
 
 uintptr
@@ -394,17 +391,17 @@
 }
 
 uintptr
-execregs(uintptr entry, ulong ssize, ulong nargs)
+execregs(uintptr entry, int argc, char *argv[], Tos *tos)
 {
-	ulong *sp;
+	ulong *sp = (void*)argv;
 	Ureg *ureg;
 
-	sp = (ulong*)(USTKTOP - ssize);
-	*--sp = nargs;
+	*--sp = argc;
 
 	ureg = up->dbgreg;
 	ureg->sp = (uintptr) sp;
 	ureg->pc = entry;
 	ureg->r14 = 0;
-	return USTKTOP-sizeof(Tos);
+
+	return (uintptr)tos;
 }
--- a/sys/src/9/kw/arch.c
+++ b/sys/src/9/kw/arch.c
@@ -92,7 +92,7 @@
 void
 procsetup(Proc* p)
 {
-	fpusysprocsetup(p);
+	fpuprocsetup(p);
 }
 
 void
--- a/sys/src/9/kw/fns.h
+++ b/sys/src/9/kw/fns.h
@@ -92,7 +92,7 @@
 extern void fpuinit(void);
 extern void fpuprocrestore(Proc*);
 extern void fpuprocsave(Proc*);
-extern void fpusysprocsetup(Proc*);
+extern void fpuprocsetup(Proc*);
 extern int fpuemu(Ureg*);
 
 /*
@@ -118,7 +118,6 @@
 extern void spldone(void);
 extern int splfhi(void);
 extern int splflo(void);
-extern void sysprocsetup(Proc*);
 extern int isaconfig(char*, int, ISAConf*);	/* only devusb.c */
 
 int	cas32(void*, u32int, u32int);
--- a/sys/src/9/mt7688/syscall.c
+++ b/sys/src/9/mt7688/syscall.c
@@ -137,20 +137,16 @@
 
 /* set up user registers before return from exec() */
 uintptr
-execregs(ulong entry, ulong ssize, ulong nargs)
+execregs(ulong entry, int argc, char *argv[], Tos *tos)
 {
+	ulong *sp = (void*)argv;
 	Ureg *ur;
-	ulong *sp;
 
-	sp = (ulong*)(USTKTOP - ssize);
-	*--sp = nargs;
+	*--sp = argc;
 
 	ur = (Ureg*)up->dbgreg;
 	ur->usp = (ulong)sp;
-	ur->pc = entry - 4;		/* syscall advances it */
+	ur->pc = entry - 4;	/* syscall advances it */
 
-//	iprint("%lud: %s EXECREGS pc %#luX sp %#luX nargs %ld", up->pid, up->text, ur->pc, ur->usp, nargs);
-//	delay(20);
-
-	return USTKTOP-sizeof(Tos);	/* address of kernel/user shared data */
+	return (uintptr)tos;	/* address of kernel/user shared data */
 }
--- a/sys/src/9/mtx/trap.c
+++ b/sys/src/9/mtx/trap.c
@@ -502,21 +502,22 @@
 }
 
 uintptr
-execregs(uintptr entry, ulong ssize, ulong nargs)
+execregs(uintptr entry, int argc, char *argv[], Tos *tos)
 {
-	ulong *sp;
+	ulong *sp = (void*)argv;
 	Ureg *ureg;
 
-	sp = (ulong*)(USTKTOP - ssize);
-	*--sp = nargs;
+	*--sp = argc;
 
 	ureg = up->dbgreg;
 	ureg->usp = (ulong)sp;
 	ureg->pc = entry;
-	ureg->srr1 &= ~MSR_FP;
-	return USTKTOP-sizeof(Tos);		/* address of kernel/user shared data */
+	ureg->srr1 &= ~MSR_FP;	/* disable floating point */
+
+	return (uintptr)tos;	/* address of kernel/user shared data */
 }
 
+
 void
 forkchild(Proc *p, Ureg *ur)
 {
@@ -533,10 +534,7 @@
 uintptr
 userpc(void)
 {
-	Ureg *ureg;
-
-	ureg = (Ureg*)up->dbgreg;
-	return ureg->pc;
+	return up->dbgreg->pc;
 }
 
 
--- a/sys/src/9/omap/arch.c
+++ b/sys/src/9/omap/arch.c
@@ -92,7 +92,7 @@
 void
 procsetup(Proc* p)
 {
-	fpusysprocsetup(p);
+	fpuprocsetup(p);
 }
 
 void
--- a/sys/src/9/omap/fns.h
+++ b/sys/src/9/omap/fns.h
@@ -107,7 +107,7 @@
 extern void fpuinit(void);
 extern void fpuprocrestore(Proc*);
 extern void fpuprocsave(Proc*);
-extern void fpusysprocsetup(Proc*);
+extern void fpuprocsetup(Proc*);
 extern void fpuprocfork(Proc*);
 extern int fpuemu(Ureg*);
 
@@ -131,8 +131,6 @@
 extern void evenaddr(uintptr);
 extern void idlehands(void);
 extern void setkernur(Ureg*, Proc*);		/* only devproc.c */
-extern void* sysexecregs(uintptr, ulong, int);
-extern void sysprocsetup(Proc*);
 
 /*
  * PCI stuff.
--- a/sys/src/9/omap/softfpu.c
+++ b/sys/src/9/omap/softfpu.c
@@ -80,11 +80,11 @@
 }
 
 void
-fpusysprocsetup(Proc*)
+fpuprocsetup(Proc*)
 {
 	/*
 	 * Disable the FPU.
-	 * Called from sysexec() via sysprocsetup() to
+	 * Called from sysexec() via procsetup() to
 	 * set the FPU for the new process.
 	 */
 }
--- a/sys/src/9/omap/syscall.c
+++ b/sys/src/9/omap/syscall.c
@@ -96,31 +96,18 @@
 }
 
 uintptr
-execregs(uintptr entry, ulong ssize, ulong nargs)
+execregs(uintptr entry, int argc, char *argv[], Tos *tos)
 {
-	ulong *sp;
+	ulong *sp = (void*)argv;
 	Ureg *ureg;
 
-	sp = (ulong*)(USTKTOP - ssize);
-	*--sp = nargs;
+	*--sp = argc;
 
 	ureg = up->dbgreg;
-//	memset(ureg, 0, 15*sizeof(ulong));
 	ureg->r13 = (ulong)sp;
 	ureg->pc = entry;
-//print("%lud: EXECREGS pc %#ux sp %#ux nargs %ld\n", up->pid, ureg->pc, ureg->r13, nargs);
 
-	/*
-	 * return the address of kernel/user shared data
-	 * (e.g. clock stuff)
-	 */
-	return USTKTOP-sizeof(Tos);
-}
-
-void
-sysprocsetup(Proc* p)
-{
-	fpusysprocsetup(p);
+	return (uintptr)tos;
 }
 
 /* 
--- a/sys/src/9/pc/trap.c
+++ b/sys/src/9/pc/trap.c
@@ -518,13 +518,12 @@
 }
 
 uintptr
-execregs(uintptr entry, ulong ssize, ulong nargs)
+execregs(uintptr entry, int argc, char *argv[], Tos *tos)
 {
-	ulong *sp;
+	ulong *sp = (void*)argv;
 	Ureg *ureg;
 
-	sp = (ulong*)(USTKTOP - ssize);
-	*--sp = nargs;
+	*--sp = argc;
 
 	ureg = up->dbgreg;
 	ureg->usp = (ulong)sp;
@@ -532,7 +531,8 @@
 	ureg->cs = UESEL;
 	ureg->ss = ureg->ds = ureg->es = UDSEL;
 	ureg->fs = ureg->gs = NULLSEL;
-	return USTKTOP-sizeof(Tos);		/* address of kernel/user shared data */
+
+	return (uintptr)tos;		/* address of kernel/user shared data */
 }
 
 /*
@@ -541,10 +541,7 @@
 uintptr
 userpc(void)
 {
-	Ureg *ureg;
-
-	ureg = (Ureg*)up->dbgreg;
-	return ureg->pc;
+	return up->dbgreg->pc;
 }
 
 /* This routine must save the values of registers the user is not permitted
--- a/sys/src/9/pc64/trap.c
+++ b/sys/src/9/pc64/trap.c
@@ -498,13 +498,13 @@
 }
 
 uintptr
-execregs(uintptr entry, ulong ssize, ulong nargs)
+execregs(uintptr entry, int argc, char *argv[], Tos *tos)
 {
-	uintptr *sp;
+	uintptr *sp = (void*)argv;
 	Ureg *ureg;
 
-	sp = (uintptr*)(USTKTOP - ssize);
-	*--sp = nargs;
+	*--sp = argc;
+
 	ureg = up->dbgreg;
 	ureg->sp = (uintptr)sp;
 	ureg->pc = entry;
@@ -511,7 +511,8 @@
 	ureg->cs = UESEL;
 	ureg->ss = UDSEL;
 	ureg->r14 = ureg->r15 = 0;	/* extern user registers */
-	return (uintptr)USTKTOP-sizeof(Tos);		/* address of kernel/user shared data */
+
+	return (uintptr)tos;	/* address of kernel/user shared data */
 }
 
 /*
@@ -520,10 +521,7 @@
 uintptr
 userpc(void)
 {
-	Ureg *ureg;
-
-	ureg = (Ureg*)up->dbgreg;
-	return ureg->pc;
+	return up->dbgreg->pc;
 }
 
 /* This routine must save the values of registers the user is not permitted
--- a/sys/src/9/port/portdat.h
+++ b/sys/src/9/port/portdat.h
@@ -47,6 +47,7 @@
 typedef struct Sema	Sema;
 typedef struct Timer	Timer;
 typedef struct Timers	Timers;
+typedef struct Tos	Tos;
 typedef struct Uart	Uart;
 typedef struct Waitq	Waitq;
 typedef struct Walkqid	Walkqid;
@@ -60,6 +61,7 @@
 #pragma incomplete Mntrpc
 #pragma incomplete Queue
 #pragma incomplete Timers
+#pragma incomplete Tos
 
 #include <fcall.h>
 
@@ -796,8 +798,8 @@
 	uintptr	qpc;		/* pc calling last blocking qlock */
 	QLock	*eql;		/* interruptable eqlock */
 
-	void	*noteureg;	/* User registers for notes */
-	void	*dbgreg;	/* User registers for devproc */
+	Ureg	*noteureg;	/* User registers for notes */
+	Ureg	*dbgreg;	/* User registers for devproc */
 
 	PFPU;			/* machine specific fpu state */
 	PMMU;			/* machine specific mmu state */
--- a/sys/src/9/port/portfns.h
+++ b/sys/src/9/port/portfns.h
@@ -107,7 +107,7 @@
 int		eqqid(Qid, Qid);
 _Noreturn void	error(char*);
 void		eqlock(QLock*);
-uintptr		execregs(uintptr, ulong, ulong);
+uintptr		execregs(uintptr, int, char**, Tos*);
 void		exhausted(char*);
 void		exit(int);
 uvlong		fastticks(uvlong*);
--- a/sys/src/9/port/sysproc.c
+++ b/sys/src/9/port/sysproc.c
@@ -310,8 +310,8 @@
 	} u;
 	char *progarg[32+1], **argv, **argp;
 	char *file, *elem, *args, *charp, *a, *e;
-	int i, n, indir, nargs;
-	ulong magic, ssize, nbytes, argc;
+	int i, n, indir, nargs, argc;
+	ulong magic, ssize, nbytes;
 	uintptr entry, text, data, bss, adata, abss, ebss, tstk, align;
 	Segment *s, *ts;
 	Image *img;
@@ -509,8 +509,10 @@
 			validaddr((uintptr)&argp[i+1], BY2WD, 0);
 	}
 	argc += i;
+	if(argc < 1)
+		error(Ebadarg);
 
-	ssize = BY2WD*(argc+1) + ((nbytes+(BY2WD-1)) & ~(BY2WD-1));
+	ssize = BY2WD*((ulong)argc+1) + ((nbytes+(BY2WD-1)) & ~(BY2WD-1));
 
 	/*
 	 * 8-byte align SP for those (e.g. sparc) that need it.
@@ -702,7 +704,11 @@
 
 	if(up->hang)
 		up->procctl = Proc_stopme;
-	return execregs(entry, ssize, argc);
+
+	tos = (Tos*)(USTKTOP - sizeof(Tos));
+	argv = (char**)(USTKTOP - ssize);
+
+	return execregs(entry, argc, argv, tos);
 }
 
 int
--- a/sys/src/9/ppc/trap.c
+++ b/sys/src/9/ppc/trap.c
@@ -477,20 +477,19 @@
 }
 
 uintptr
-execregs(uintptr entry, ulong ssize, ulong nargs)
+execregs(uintptr entry, int argc, char *argv[], Tos *tos)
 {
-	ulong *sp;
+	ulong *sp = (void*)argv;
 	Ureg *ureg;
 
-	sp = (ulong*)(USTKTOP - ssize);
-	*--sp = nargs;
+	*--sp = argc;
 
 	ureg = up->dbgreg;
 	ureg->usp = (ulong)sp;
 	ureg->pc = entry;
-	ureg->srr1 &= ~MSR_FP;		/* disable floating point */
+	ureg->srr1 &= ~MSR_FP;	/* disable floating point */
 
-	return USTKTOP-sizeof(Tos);		/* address of kernel/user shared data */
+	return (uintptr)tos;	/* address of kernel/user shared data */
 }
 
 void
@@ -509,10 +508,7 @@
 uintptr
 userpc(void)
 {
-	Ureg *ureg;
-
-	ureg = (Ureg*)up->dbgreg;
-	return ureg->pc;
+	return up->dbgreg->pc;
 }
 
 
--- a/sys/src/9/sgi/trap.c
+++ b/sys/src/9/sgi/trap.c
@@ -579,27 +579,24 @@
 
 /* set up user registers before return from exec() */
 uintptr
-execregs(ulong entry, ulong ssize, ulong nargs)
+execregs(uintptr entry, int argc, char *argv[], Tos *tos)
 {
+	uintptr *sp = (void*)argv;
 	Ureg *ur;
-	ulong *sp;
 
-	sp = (ulong*)(USTKTOP - ssize);
-	*--sp = nargs;
+	*--sp = argc;
 
-	ur = (Ureg*)up->dbgreg;
-	ur->usp = (ulong)sp;
+	ur = up->dbgreg;
+	ur->usp = (uintptr)sp;
 	ur->pc = entry - 4;		/* syscall advances it */
-	return USTKTOP-sizeof(Tos);	/* address of kernel/user shared data */
+
+	return (uintptr)tos;	/* address of kernel/user shared data */
 }
 
 ulong
 userpc(void)
 {
-	Ureg *ur;
-
-	ur = (Ureg*)up->dbgreg;
-	return ur->pc;
+	return up->dbgreg->pc;
 }
 
 /*
--- a/sys/src/9/teg2/arch.c
+++ b/sys/src/9/teg2/arch.c
@@ -92,7 +92,7 @@
 void
 procsetup(Proc* p)
 {
-	fpusysprocsetup(p);
+	fpuprocsetup(p);
 }
 
 /*
--- a/sys/src/9/teg2/fns.h
+++ b/sys/src/9/teg2/fns.h
@@ -146,7 +146,7 @@
 extern void fpuinit(void);
 extern void fpuprocrestore(Proc*);
 extern void fpuprocsave(Proc*);
-extern void fpusysprocsetup(Proc*);
+extern void fpuprocsetup(Proc*);
 extern void fpuprocfork(Proc*);
 extern int fpuemu(Ureg*);
 
@@ -171,8 +171,6 @@
 extern void evenaddr(uintptr);
 extern void idlehands(void);
 extern void setkernur(Ureg*, Proc*);		/* only devproc.c */
-extern void* sysexecregs(uintptr, ulong, int);
-extern void sysprocsetup(Proc*);
 
 /* libc */
 long labs(long);
--- a/sys/src/9/xen/trap.c
+++ b/sys/src/9/xen/trap.c
@@ -445,19 +445,18 @@
 }
 
 uintptr
-execregs(uintptr entry, ulong ssize, ulong nargs)
+execregs(uintptr entry, int argc, char *argv[], Tos *tos)
 {
-	ulong *sp;
+	ulong *sp = (void*)argv;
 	Ureg *ureg;
 
-	sp = (ulong*)(USTKTOP - ssize);
-	*--sp = nargs;
+	*--sp = argc;
 
 	ureg = up->dbgreg;
 	ureg->usp = (ulong)sp;
 	ureg->pc = entry;
-//	print("execregs returns 0x%x\n", USTKTOP-sizeof(Tos));
-	return USTKTOP-sizeof(Tos);		/* address of kernel/user shared data */
+
+	return (uintptr)tos;		/* address of kernel/user shared data */
 }
 
 /*
--- a/sys/src/9/zynq/trap.c
+++ b/sys/src/9/zynq/trap.c
@@ -380,10 +380,7 @@
 uintptr
 userpc(void)
 {
-	Ureg *ur;
-	
-	ur = up->dbgreg;
-	return ur->pc;
+	return up->dbgreg->pc;
 }
 
 uintptr
@@ -485,17 +482,17 @@
 }
 
 uintptr
-execregs(uintptr entry, ulong ssize, ulong nargs)
+execregs(uintptr entry, int argc, char *argv[], Tos *tos)
 {
-	ulong *sp;
+	ulong *sp = (void*)argv;
 	Ureg *ureg;
 
-	sp = (ulong*)(USTKTOP - ssize);
-	*--sp = nargs;
+	*--sp = argc;
 
 	ureg = up->dbgreg;
 	ureg->sp = (uintptr) sp;
 	ureg->pc = entry;
 	ureg->r14 = 0;
-	return USTKTOP-sizeof(Tos);
+
+	return (uintptr)tos;
 }
--