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

Re: [MiNT] Corruption of high TPA



This is a patch for the MiNTLib as a workaround to that OS bug.

With this patch, the programs linked with the MiNTLib works again with memory protection enabled.

And now we can use safely the early stack, for example by displaying debug messages, without crashing the program !

Please commit.

--
Vincent Rivière
diff -aurN -x CVS mintlib.orig/mintlib/setstack.S mintlib/mintlib/setstack.S
--- mintlib.orig/mintlib/setstack.S	2000-10-12 12:56:41.000000000 +0200
+++ mintlib/mintlib/setstack.S	2009-06-12 23:03:32.984375000 +0200
@@ -13,6 +13,16 @@
 |     void setstack( void *newsp )
 | called from crtinit.c once the new stack size has been decided upon
 |
+| Note: It is strictly forbidden to read or write data at *newsp or beyond.
+| Unfortunately, there are known bugs in the trap #1 handlers of most
+| operating systems. For example, older versions of the MiNT kernel
+| reads unconditionally 36 bytes from the user stack on any trap #1, even
+| if there are no parameters at all. So if *newsp is actually unreadable
+| (end of RAM, MMU protected...), the program will crash on the first
+| trap #1 call.
+| As a workaround, we can waste some space at the top of the new stack
+| to ensure it will be readable by the buggy OS. 64 bytes should be enough.
+|
 | WARNING WARNING WARNING: after you do this, local variables may no longer
 | be accessible!
 | destroys a0 and a7
@@ -21,6 +31,7 @@
 __setstack:
 	movel	sp@+, a0	| save return address
 	movel	sp@, sp		| new stack pointer
-	subql	#4, sp		| fixup for tidy upon return
+	subl	#64+4, sp	| push some unused space for buggy OS and a
+				| fake parameter to be popped by the caller
 	jmp	a0@		| back to caller
 
diff -aurN -x CVS mintlib.orig/startup/crt0.S mintlib/startup/crt0.S
--- mintlib.orig/startup/crt0.S	2009-06-04 11:49:24.875000000 +0200
+++ mintlib/startup/crt0.S	2009-06-12 23:03:07.828125000 +0200
@@ -69,6 +69,7 @@
 	movel	a0@(4), d0	| get _base->p_hitpa
 	bclr	#0, d0		| round off
 	movel	d0, sp		| set stack (temporarily)
+	subl	#64, sp		| see note in mintlib/setstack.S
 	jsr	.early_init	| early initialization
 	jsr	__checkcpu	| check for correct CPU or exit
 	jmp	__crtinit	| in crtinit.c