[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [MiNT] Super() bug



Vincent Rivière wrote:
*Rule:* If the stack is different than the value it was at 1), the stack
value after the trap is totally bogus !!

I'm tired to hit that bug again and again on TOS. Recently, with SDL.

So Alan, please commit the attached patch with the following CVS comment:
New safe binding SuperToUser() to return from supervisor to user mode.
Patch provided by Vincent Rivière.

Then we will have to patch the libraries accordingly.

--
Vincent Rivière
diff -aurN -x CVS mintlib.orig/include/mint/osbind.h mintlib/include/mint/osbind.h
--- mintlib.orig/include/mint/osbind.h	2009-05-22 22:35:36.671875000 +0200
+++ mintlib/include/mint/osbind.h	2011-02-22 23:08:49.781250000 +0100
@@ -983,6 +983,35 @@
 #define	       Super(ptr)					       \
        (long)trap_1_wl((short)(0x20),(long)(ptr))
 	/* Tos 1.4: Super(1L) : rets -1L if in super mode, 0L otherwise */
+
+/*
+ * Safe binding to switch back from supervisor to user mode.
+ * On TOS or EmuTOS, if the stack pointer has changed between Super(0)
+ * and Super(oldssp), the resulting user stack pointer is wrong.
+ * This bug does not occur with FreeMiNT.
+ * So the safe way to return from supervisor to user mode is to backup
+ * the stack pointer then restore it after the trap.
+ * Sometimes, GCC optimizes the stack usage, so this matters.
+ */
+#define SuperToUser(ptr)						\
+(void)__extension__							\
+({									\
+	register long retvalue __asm__("d0");				\
+	register long sp_backup;					\
+									\
+	__asm__ volatile						\
+	(								\
+		"movl	sp,%1\n\t"					\
+		"movl	%2,sp@-\n\t"					\
+		"movw	#0x20,sp@-\n\t"					\
+		"trap	#1\n\t"						\
+		"movl	%1,sp\n\t"					\
+	: "=r"(retvalue), "=&r"(sp_backup)	/* outputs */		\
+	: "g"((long)(ptr)) 			/* inputs */		\
+	: __CLOBBER_RETURN("d0") "d1", "d2", "a0", "a1", "a2"		\
+	);								\
+})
+
 #define	       Tgetdate()					       \
        (short)trap_1_w((short)(0x2A))
 #define	       Tsetdate(date)					       \
diff -aurN -x CVS mintlib.orig/mintlib/setsysvar.c mintlib/mintlib/setsysvar.c
--- mintlib.orig/mintlib/setsysvar.c	2009-06-04 11:18:41.000000000 +0200
+++ mintlib/mintlib/setsysvar.c	2011-02-22 23:11:01.875000000 +0100
@@ -13,7 +13,7 @@
 
     		save_ssp = (long) Super((void *) 0L);
     		*((volatile long *)var) = val;
-    		(void)Super((void *) save_ssp);
+    		(void)SuperToUser((void *) save_ssp);
 	}
 	else
 		(void) Ssystem (S_SETLVAL, var, val); /* note: root only! */
diff -aurN -x CVS mintlib.orig/unix/ioctl.c mintlib/unix/ioctl.c
--- mintlib.orig/unix/ioctl.c	2008-09-29 17:35:27.000000000 +0200
+++ mintlib/unix/ioctl.c	2011-02-22 23:11:48.437500000 +0100
@@ -215,7 +215,7 @@
 		  		if (Super(1L) == 0L) {
 		  			ssp = Super(0L);
                 	m = *mfp & 0xff;
-                  	Super(ssp);
+                  	SuperToUser(ssp);
                 } else {
                 	m = *mfp & 0xff;
                 }
diff -aurN -x CVS mintlib.orig/unix/sysinfo.c mintlib/unix/sysinfo.c
--- mintlib.orig/unix/sysinfo.c	2009-10-13 21:35:54.906250000 +0200
+++ mintlib/unix/sysinfo.c	2011-02-22 23:12:29.125000000 +0100
@@ -302,7 +302,7 @@
 				save_stk = (void *) Super (0L);
 				sysbase = *((long int**) 0x000004f2);
 				tosversion = *sysbase;
-				(void) Super ((void*) save_stk);
+				(void) SuperToUser ((void*) save_stk);
 			}
 			else
 			{