shithub: riscv

Download patch

ref: 14685d65958bed4d68b9c60564fa8d09aa746462
parent: 7657312dcf5b9435ff848b20dfc3a0546ab8a500
author: cinap_lenrek <[email protected]>
date: Tue May 3 20:23:48 EDT 2016

ape: return plan9 error strings from strerror()

when _syserrno() fails to map a plan9 error string to
a unix error number, we copy the plan9 error string
to the per process error buffer "plan9err" and set
errno = EPLAN9.

when strerror() is called with EPLAN9, it returns
a pointer to the plan9err buffer.

--- a/sys/src/ape/lib/ap/386/main9.s
+++ b/sys/src/ape/lib/ap/386/main9.s
@@ -2,16 +2,19 @@
 
 GLOBL	_tos(SB), $4
 GLOBL	_errnoloc(SB), $4
+GLOBL	_plan9err(SB), $4
 GLOBL	_privates(SB), $4
 GLOBL	_nprivates(SB), $4
 
-TEXT	_main(SB), 1, $(16+NPRIVATES*4)
+TEXT	_main(SB), 1, $(12+4+128+NPRIVATES*4)
 
 	/* _tos = arg */
 	MOVL	AX, _tos(SB)
 	LEAL	12(SP), AX
 	MOVL	AX, _errnoloc(SB)
-	LEAL	16(SP), AX
+	ADDL	$4, AX
+	MOVL	AX, _plan9err(SB)
+	ADDL	$128, AX
 	MOVL	AX, _privates(SB)
 	MOVL	$NPRIVATES, _nprivates(SB)
 
--- a/sys/src/ape/lib/ap/386/main9p.s
+++ b/sys/src/ape/lib/ap/386/main9p.s
@@ -2,16 +2,19 @@
 
 GLOBL	_tos(SB), $4
 GLOBL	_errnoloc(SB), $4
+GLOBL	_plan9err(SB), $4
 GLOBL	_privates(SB), $4
 GLOBL	_nprivates(SB), $4
 
-TEXT	_mainp(SB), 1, $(16+NPRIVATES*4)
+TEXT	_mainp(SB), 1, $(12+4+128+NPRIVATES*4)
 
 	/* _tos = arg */
 	MOVL	AX, _tos(SB)
 	LEAL	12(SP), AX
 	MOVL	AX, _errnoloc(SB)
-	LEAL	16(SP), AX
+	ADDL	$4, AX
+	MOVL	AX, _plan9err(SB)
+	ADDL	$128, AX
 	MOVL	AX, _privates(SB)
 	MOVL	$NPRIVATES, _nprivates(SB)
 
--- a/sys/src/ape/lib/ap/68020/main9.s
+++ b/sys/src/ape/lib/ap/68020/main9.s
@@ -2,22 +2,32 @@
 
 GLOBL	_tos(SB), $4
 GLOBL	_errnoloc(SB), $4
+GLOBL	_plan9err(SB), $4
 GLOBL	_privates(SB), $4
 GLOBL	_nprivates(SB), $4
 
-TEXT	_main(SB), 1, $(16+NPRIVATES*4)
+TEXT	_main(SB), 1, $(4+128+NPRIVATES*4)
 	MOVL	$a6base(SB), A6
 
 	/* _tos = arg */
 	MOVL	R0, _tos(SB)		/* return value of sys exec!! */
-	LEA	errno+12(SB), _errnoloc(SB)
-	LEA	private+16(SP), _privates(SB)
+
+	MOVL	A7, A1
+	MOVL	A1, _errnoloc(SB)
+	ADDL	$4, A1
+	MOVL	A1, _plan9err(SB)
+	ADDL	$128, A1
+	MOVL	A1, _privates(SB)
 	MOVL	$NPRIVATES, _nprivates(SB)
 
+	BSR	_envsetup(SB)
+
+	/* main(argc, argv, environ); */
+	MOVL	environ(SB), TOS
 	PEA	inargv+0(FP)
 	MOVL	inargc-4(FP), TOS
-	BSR	_envsetup(SB)
 	BSR	main(SB)
+
 	MOVL	R0,TOS
 	BSR	exit(SB)
 	RTS
--- a/sys/src/ape/lib/ap/68020/main9p.s
+++ b/sys/src/ape/lib/ap/68020/main9p.s
@@ -2,16 +2,22 @@
 
 GLOBL	_tos(SB), $4
 GLOBL	_errnoloc(SB), $4
+GLOBL	_plan9err(SB), $4
 GLOBL	_privates(SB), $4
 GLOBL	_nprivates(SB), $4
 
-TEXT	_mainp(SB), 1, $(16+NPRIVATES*4)
+TEXT	_mainp(SB), 1, $(4+128+NPRIVATES*4)
 	MOVL	$a6base(SB), A6
 
 	/* _tos = arg */
 	MOVL	R0, _tos(SB)		/* return value of sys exec!! */
-	LEA	errno+12(SP), _errnoloc(SB)
-	LEA	private+16(SP), _privates(SB)
+
+	MOVL	A7, A1
+	MOVL	A1, _errnoloc(SB)
+	ADDL	$4, A1
+	MOVL	A1, _plan9err(SB)
+	ADDL	$128, A1
+	MOVL	A1, _privates(SB)
 	MOVL	$NPRIVATES, _nprivates(SB)
 
 	/* _profmain(); */
--- a/sys/src/ape/lib/ap/alpha/main9.s
+++ b/sys/src/ape/lib/ap/alpha/main9.s
@@ -2,26 +2,34 @@
 
 GLOBL	_tos(SB), $4
 GLOBL	_errnoloc(SB), $4
+GLOBL	_plan9err(SB), $4
 GLOBL	_privates(SB), $4
 GLOBL	_nprivates(SB), $4
 
-TEXT	_main(SB), 1, $(16+NPRIVATES*4)
+TEXT	_main(SB), 1, $(20+4+128+NPRIVATES*4)
 	MOVQ	$setSB(SB), R29
 
 	/* _tos = arg */
 	MOVL	R0, _tos(SB)
-	MOVQ	$12(SP), R1
+
+	MOVL	$20(R30), R1
 	MOVL	R1, _errnoloc(SB)
-	MOVQ	$16(SP), R1
+	ADDL	$4, R1
+	MOVL	R1, _plan9err(SB)
+	ADDL	$128, R1
 	MOVL	R1, _privates(SB)
 	MOVQ	$NPRIVATES, R1
 	MOVL	R1, _nprivates(SB)
 
 	JSR	_envsetup(SB)
-	MOVL	inargc-8(FP), R0
-	MOVL	$inargv-4(FP), R1
+
+	/* main(argc, argv, environ); */
+	MOVL	inargc-4(FP), R0
+	MOVL	$inargv+0(FP), R1
+	MOVL	environ(SB), R2
 	MOVL	R0, 8(R30)
 	MOVL	R1, 12(R30)
+	MOVL	R2, 16(R30)
 	JSR	main(SB)
 loop:
 	MOVL	R0, 8(R30)
--- a/sys/src/ape/lib/ap/alpha/main9p.s
+++ b/sys/src/ape/lib/ap/alpha/main9p.s
@@ -2,17 +2,21 @@
 
 GLOBL	_tos(SB), $4
 GLOBL	_errnoloc(SB), $4
+GLOBL	_plan9err(SB), $4
 GLOBL	_privates(SB), $4
 GLOBL	_nprivates(SB), $4
 
-TEXT	_mainp(SB), 1, $(16+NPRIVATES*4)
+TEXT	_mainp(SB), 1, $(20+4+128+NPRIVATES*4)
 	MOVQ	$setSB(SB), R29
 
 	/* _tos = arg */
 	MOVL	R0, _tos(SB)
-	MOVQ	$12(SP), R1
+
+	MOVL	$20(R30), R1
 	MOVL	R1, _errnoloc(SB)
-	MOVQ	$16(SP), R1
+	ADDL	$4, R1
+	MOVL	R1, _plan9err(SB)
+	ADDL	$128, R1
 	MOVL	R1, _privates(SB)
 	MOVQ	$NPRIVATES, R1
 	MOVL	R1, _nprivates(SB)
--- a/sys/src/ape/lib/ap/amd64/main9.s
+++ b/sys/src/ape/lib/ap/amd64/main9.s
@@ -2,19 +2,25 @@
 
 GLOBL	_tos(SB), $8
 GLOBL	_errnoloc(SB), $8
+GLOBL	_plan9err(SB), $8
 GLOBL	_privates(SB), $8
 GLOBL	_nprivates(SB), $8
 
-TEXT	_main(SB), 1, $(32+NPRIVATES*8)
+TEXT	_main(SB), 1, $(24+8+128+NPRIVATES*8)
 
 	/* _tos = arg */
 	MOVQ	AX, _tos(SB)
+
 	LEAQ	24(SP), AX
 	MOVQ	AX, _errnoloc(SB)
-	LEAQ	32(SP), AX
+	ADDQ	$8, AX
+	MOVQ	AX, _plan9err(SB)
+	ADDQ	$128, AX
 	MOVQ	AX, _privates(SB)
 	MOVQ	$NPRIVATES, _nprivates(SB)
+
 	CALL	_envsetup(SB)
+
 	MOVL	inargc-8(FP), RARG
 	LEAQ	inargv+0(FP), AX
 	MOVQ	AX, 8(SP)
@@ -21,6 +27,7 @@
 	MOVQ	environ(SB), AX
 	MOVQ	AX, 16(SP)
 	CALL	main(SB)
+
 	MOVQ	AX, RARG
 	CALL	exit(SB)
 	RET
--- a/sys/src/ape/lib/ap/amd64/main9p.s
+++ b/sys/src/ape/lib/ap/amd64/main9p.s
@@ -1,14 +1,21 @@
 #define NPRIVATES	16
 
 GLOBL	_tos(SB), $8
+GLOBL	_errnoloc(SB), $8
+GLOBL	_plan9err(SB), $8
 GLOBL	_privates(SB), $8
 GLOBL	_nprivates(SB), $8
 
-TEXT	_mainp(SB), 1, $(3*8+NPRIVATES*8)
+TEXT	_mainp(SB), 1, $(24+8+128+NPRIVATES*8)
 
 	/* _tos = arg */
 	MOVQ	AX, _tos(SB)
-	LEAQ	8(SP), AX
+
+	LEAQ	24(SP), AX
+	MOVQ	AX, _errnoloc(SB)
+	ADDQ	$8, AX
+	MOVQ	AX, _plan9err(SB)
+	ADDQ	$128, AX
 	MOVQ	AX, _privates(SB)
 	MOVQ	$NPRIVATES, _nprivates(SB)
 
--- a/sys/src/ape/lib/ap/arm/main9.s
+++ b/sys/src/ape/lib/ap/arm/main9.s
@@ -6,18 +6,22 @@
 
 GLOBL	_tos(SB), $4
 GLOBL	_errnoloc(SB), $4
+GLOBL	_plan9err(SB), $4
 GLOBL	_privates(SB), $4
 GLOBL	_nprivates(SB), $4
 
-TEXT	_main(SB), 1, $(16+NPRIVATES*4)
+TEXT	_main(SB), 1, $(16+4+128+NPRIVATES*4)
 
 	MOVW	$setR12(SB), R(sb)
 
 	/* _tos = arg */
 	MOVW	R(arg), _tos(SB)
-	MOVW	$errno-68(SP), R1
+
+	MOVW	$16(R(sp)), R1
 	MOVW	R1, _errnoloc(SB)
-	MOVW	$private-64(SP), R1
+	ADD	$4, R1
+	MOVW	R1, _plan9err(SB)
+	ADD	$128, R1
 	MOVW	R1, _privates(SB)
 	MOVW	$NPRIVATES, R1
 	MOVW	R1, _nprivates(SB)
--- a/sys/src/ape/lib/ap/arm/main9p.s
+++ b/sys/src/ape/lib/ap/arm/main9p.s
@@ -6,18 +6,22 @@
 
 GLOBL	_tos(SB), $4
 GLOBL	_errnoloc(SB), $4
+GLOBL	_plan9err(SB), $4
 GLOBL	_privates(SB), $4
 GLOBL	_nprivates(SB), $4
 
-TEXT	_mainp(SB), 1, $(16+NPRIVATES*4)
+TEXT	_mainp(SB), 1, $(16+4+128+NPRIVATES*4)
 
 	MOVW	$setR12(SB), R(sb)
 
 	/* _tos = arg */
 	MOVW	R(arg), _tos(SB)
-	MOVW	$errno-68(SP), R1
+
+	MOVW	$16(R(sp)), R1
 	MOVW	R1, _errnoloc(SB)
-	MOVW	$private-64(SP), R1
+	ADD	$4, R1
+	MOVW	R1, _plan9err(SB)
+	ADD	$128, R1
 	MOVW	R1, _privates(SB)
 	MOVW	$NPRIVATES, R1
 	MOVW	R1, _nprivates(SB)
--- a/sys/src/ape/lib/ap/mips/main9.s
+++ b/sys/src/ape/lib/ap/mips/main9.s
@@ -2,18 +2,21 @@
 
 GLOBL	_tos(SB), $4
 GLOBL	_errnoloc(SB), $4
+GLOBL	_plan9err(SB), $4
 GLOBL	_privates(SB), $4
 GLOBL	_nprivates(SB), $4
 
-TEXT	_main(SB), 1, $(16+NPRIVATES*4)
+TEXT	_main(SB), 1, $(16+4+128+NPRIVATES*4)
 	MOVW	$setR30(SB), R30
 
 	/* _tos = arg */
 	MOVW	R1, _tos(SB)
 
-	MOVW	$p-68(SP), R1
+	MOVW	$16(R29), R1
 	MOVW	R1, _errnoloc(SB)
 	ADDU	$4, R1
+	MOVW	R1, _plan9err(SB)
+	ADDU	$128, R1
 	MOVW	R1, _privates(SB)
 	MOVW	$NPRIVATES, R1
 	MOVW	R1, _nprivates(SB)
--- a/sys/src/ape/lib/ap/mips/main9p.s
+++ b/sys/src/ape/lib/ap/mips/main9p.s
@@ -2,20 +2,23 @@
 
 GLOBL	_tos(SB), $4
 GLOBL	_errnoloc(SB), $4
+GLOBL	_plan9err(SB), $4
 GLOBL	_privates(SB), $4
 GLOBL	_nprivates(SB), $4
 
-TEXT	_mainp(SB), 1, $(16+NPRIVATES*4)
+TEXT	_mainp(SB), 1, $(16+4+128+NPRIVATES*4)
 	MOVW	$setR30(SB), R30
 
 	/* _tos = arg */
 	MOVW	R1, _tos(SB)
 
-	MOVW	$p-68(SP), R1
+	MOVW	$16(R29), R1
 	MOVW	R1, _errnoloc(SB)
 	ADDU	$4, R1
+	MOVW	R1, _plan9err(SB)
+	ADDU	$128, R1
 	MOVW	R1, _privates(SB)
-	MOVW	$(NPRIVATES-1), R1
+	MOVW	$NPRIVATES, R1
 	MOVW	R1, _nprivates(SB)
 
 	/* _profmain(); */
--- a/sys/src/ape/lib/ap/plan9/_errno.c
+++ b/sys/src/ape/lib/ap/plan9/_errno.c
@@ -7,7 +7,7 @@
 
 /* see also: ../stdio/strerror.c, with errno-> string mapping */
 
-char _plan9err[ERRMAX];
+extern char *_plan9err;
 
 static struct errmap {
 	int	num;
@@ -110,15 +110,19 @@
 void
 _syserrno(void)
 {
+	char err[ERRMAX];
 	int i;
 
-	if(_ERRSTR(_plan9err, sizeof _plan9err) < 0)
-		errno = EINVAL;
-	else{
-		for(i = 0; i < NERRMAP; i++)
-			if(strstr(_plan9err, map[i].ename) != 0)
-				break;
-		_ERRSTR(_plan9err, sizeof _plan9err);
-		errno = (i < NERRMAP)? map[i].num : EINVAL;
+	err[0] = 0;
+	_ERRSTR(err, sizeof err);
+	strncpy(_plan9err, err, sizeof err);
+	_plan9err[sizeof err-1] = 0;
+	errno = EPLAN9;
+	for(i = 0; i < NERRMAP; i++){
+		if(strstr(err, map[i].ename) != 0){
+			errno = map[i].num;
+			break;
+		}
 	}
+	_ERRSTR(err, sizeof err);
 }
--- a/sys/src/ape/lib/ap/power/main9.s
+++ b/sys/src/ape/lib/ap/power/main9.s
@@ -2,27 +2,34 @@
 
 GLOBL	_tos(SB), $4
 GLOBL	_errnoloc(SB), $4
+GLOBL	_plan9err(SB), $4
 GLOBL	_privates(SB), $4
 GLOBL	_nprivates(SB), $4
 
-TEXT	_main(SB), 1, $(16+NPRIVATES*4)
+TEXT	_main(SB), 1, $(12+4+128+NPRIVATES*4)
 
 	MOVW	$setSB(SB), R2
 
 	/* _tos = arg */
 	MOVW	R3, _tos(SB)
-	MOVW	$12(SP), R1
-	MOVW	R1, _errnoloc(SB)
-	MOVW	$16(SP), R1
-	MOVW	R1, _privates(SB)
-	MOVW	$NPRIVATES, R1
-	MOVW	R1, _nprivates(SB)
 
+	MOVW	$12(R1), R3
+	MOVW	R3, _errnoloc(SB)
+	ADD	$4, R3
+	MOVW	R3, _plan9err(SB)
+	ADD	$128, R3
+	MOVW	R3, _privates(SB)
+	MOVW	$NPRIVATES, R3
+	MOVW	R3, _nprivates(SB)
+
 	BL	_envsetup(SB)
+
 	MOVW	inargc-4(FP), R3
 	MOVW	$inargv+0(FP), R4
+	MOVW	environ(SB), R5
 	MOVW	R3, 4(R1)
 	MOVW	R4, 8(R1)
+	MOVW	R5, 12(R1)
 	BL	main(SB)
 loop:
 	MOVW	R3, 4(R1)
--- a/sys/src/ape/lib/ap/power/main9p.s
+++ b/sys/src/ape/lib/ap/power/main9p.s
@@ -2,29 +2,33 @@
 
 GLOBL	_tos(SB), $4
 GLOBL	_errnoloc(SB), $4
+GLOBL	_plan9err(SB), $4
 GLOBL	_privates(SB), $4
 GLOBL	_nprivates(SB), $4
 
-TEXT	_mainp(SB), 1, $(16+NPRIVATES*4)
+TEXT	_mainp(SB), 1, $(12+4+128+NPRIVATES*4)
 
 	MOVW	$setSB(SB), R2
 
 	/* _tos = arg */
 	MOVW	R3, _tos(SB)
-	MOVW	$12(SP), R1
-	MOVW	R1, _errnoloc(SB)
-	MOVW	$16(SP), R1
-	MOVW	R1, _privates(SB)
-	MOVW	$NPRIVATES, R1
-	MOVW	R1, _nprivates(SB)
 
+	MOVW	$12(R1), R3
+	MOVW	R3, _errnoloc(SB)
+	ADD	$4, R3
+	MOVW	R3, _plan9err(SB)
+	ADD	$128, R3
+	MOVW	R3, _privates(SB)
+	MOVW	$NPRIVATES, R3
+	MOVW	R3, _nprivates(SB)
+
 	/* _profmain(); */
 	BL	_envsetup(SB)
 
 	/* _tos->prof.pp = _tos->prof.next; */
-	MOVW	_tos+0(SB),R1
-	MOVW	4(R1),R2
-	MOVW	R2,(R1)
+	MOVW	_tos+0(SB),R3
+	MOVW	4(R3),R2
+	MOVW	R2,(R3)
 
 	/* main(argc, argv, environ); */
 	MOVW	inargc-4(FP), R3
--- a/sys/src/ape/lib/ap/sparc/main9.s
+++ b/sys/src/ape/lib/ap/sparc/main9.s
@@ -2,10 +2,11 @@
 
 GLOBL	_tos(SB), $4
 GLOBL	_errnoloc(SB), $4
+GLOBL	_plan9err(SB), $4
 GLOBL	_privates(SB), $4
 GLOBL	_nprivates(SB), $4
 
-TEXT	_main(SB), 1, $(16+NPRIVATES*4)
+TEXT	_main(SB), 1, $(12+4+128+NPRIVATES*4)
 	MOVW	$setSB(SB), R2
 
 	/* _tos = arg */
@@ -17,18 +18,23 @@
 	FADDD	F26, F26, F28
 	FADDD	F28, F28, F30
 */
-	MOVW	$12(SP), R1
-	MOVW	R1, _errnoloc(SB)
-	MOVW	$16(SP), R1
-	MOVW	R1, _privates(SB)
-	MOVW	$NPRIVATES, R1
-	MOVW	R1, _nprivates(SB)
+	MOVW	$12(R1), R7
+	MOVW	R7, _errnoloc(SB)
+	ADD	$4, R7
+	MOVW	R7, _plan9err(SB)
+	ADD	$128, R7
+	MOVW	R7, _privates(SB)
+	MOVW	$NPRIVATES, R7
+	MOVW	R7, _nprivates(SB)
 
 	JMPL	_envsetup(SB)
+
 	MOVW	inargc-4(FP), R7
 	MOVW	$inargv+0(FP), R8
+	MOVW	environ(SB), R9
 	MOVW	R7, 4(R1)
 	MOVW	R8, 8(R1)
+	MOVW	R9, 12(R1)
 	JMPL	main(SB)
 
 loop:
--- a/sys/src/ape/lib/ap/sparc/main9p.s
+++ b/sys/src/ape/lib/ap/sparc/main9p.s
@@ -17,12 +17,14 @@
 	FADDD	F26, F26, F28
 	FADDD	F28, F28, F30
 */
-	MOVW	$12(SP), R1
-	MOVW	R1, _errnoloc(SB)
-	MOVW	$16(SP), R1
-	MOVW	R1, _privates(SB)
-	MOVW	$NPRIVATES, R1
-	MOVW	R1, _nprivates(SB)
+	MOVW	$12(R1), R7
+	MOVW	R7, _errnoloc(SB)
+	ADD	$4, R7
+	MOVW	R7, _plan9err(SB)
+	ADD	$128, R7
+	MOVW	R7, _privates(SB)
+	MOVW	$NPRIVATES, R7
+	MOVW	R7, _nprivates(SB)
 
 	/* _profmain(); */
 	JMPL	_profmain(SB)
@@ -38,6 +40,7 @@
 	MOVW	inargc-4(FP), R7
 	MOVW	$inargv+0(FP), R8
 	MOVW	environ(SB), R9
+	MOVW	R7, 4(R1)
 	MOVW	R8, 8(R1)
 	MOVW	R9, 12(R1)
 	JMPL	main(SB)
--- a/sys/src/ape/lib/ap/stdio/strerror.c
+++ b/sys/src/ape/lib/ap/stdio/strerror.c
@@ -81,7 +81,7 @@
 };
 #define	_IO_nerr	(sizeof sys_errlist/sizeof sys_errlist[0])
 int sys_nerr = _IO_nerr;
-extern char _plan9err[];
+extern char *_plan9err;
 
 char *
 strerror(int n)
--- a/sys/src/ape/lib/fmt/werrstr.c
+++ b/sys/src/ape/lib/fmt/werrstr.c
@@ -2,7 +2,7 @@
 #include <errno.h>
 #include "fmt.h"
 
-extern char _plan9err[128];
+extern char *_plan9err;
 
 void
 werrstr(const char *fmt, ...)
@@ -10,7 +10,7 @@
 	va_list arg;
 
 	va_start(arg, fmt);
-	snprint(_plan9err, sizeof _plan9err, fmt, arg);
+	snprint(_plan9err, 128, fmt, arg);
 	va_end(arg);
 	errno = EPLAN9;
 }