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

Re: [MiNT] [PATCH] Initialize USP to call the AES from supervisor mode



On 09/03/2013 20:26, Alan Hourihane wrote:
(*) The next issue is to ensure that xaaes.km has a valid USP. As we seen,
this is mandatory before calling the AES from supervisor mode.

As I suspected, usp was wrong.
Inside kthread_create_v(), the 2 stacks are initialized with the same value:
p2->ctxt[CURRENT].usp = p2->sysstack;

From my debug traces, usp was actually a bit higher than sp. So some stuff in the parent stack frames or local variables were trashed when calling appl_init() from supervisor mode. This did not cause trouble, since the thread exits with kthread_exit() without returning to the caller.

The attached patch safely initializes usp in the XaAES thread.
Please commit!

usp.patch
Initialize USP to call the AES from supervisor mode. Contributed by Vincent Rivière.

As a summary, there was several issues:
- We learned that calling the AES from supervisor is legal with special care
- USP was not initialized inside the XaAES thread
- There was a stack overflow in the EmuTOS AES inside appl_init() due to big Fsfirst() stack usage

I believe there is no more problem regarding to this issue.

--
Vincent Rivière
diff -x CVS -aurN freemint-1.18.orig/xaaes/src.km/k_main.c freemint-1.18/xaaes/src.km/k_main.c
--- freemint-1.18.orig/xaaes/src.km/k_main.c	2013-02-23 14:43:56.717875000 +0100
+++ freemint-1.18/xaaes/src.km/k_main.c	2013-03-09 21:13:07.883171800 +0100
@@ -1624,6 +1624,12 @@
 	int wait = 1, pferr, p_exc = -1;
 	unsigned long default_input_channels;
 	struct tty *tty;
+	uchar user_stack[100]; /* Stack to call the AES from supervisor mode */
+
+	/* The TOS AES saves the registers on USP inside trap #2 (56 bytes)
+	 * This hack is necessary to call the AES from supervisor mode.
+	 */
+	set_usp(user_stack + sizeof user_stack);
 
 #if CHECK_STACK
 	long stk = (long)get_sp();
diff -x CVS -aurN freemint-1.18.orig/xaaes/src.km/k_main.h freemint-1.18/xaaes/src.km/k_main.h
--- freemint-1.18.orig/xaaes/src.km/k_main.h	2013-02-08 00:18:22.968750000 +0100
+++ freemint-1.18/xaaes/src.km/k_main.h	2013-03-09 21:04:42.914421800 +0100
@@ -80,4 +80,16 @@
 }
 #endif
 
+/* Set the user stack pointer */
+static __inline__ void set_usp(void *p)
+{
+	__asm__ volatile
+	(
+		"move.l	%0,usp"
+	:		/* outputs */
+	: "a"(p)	/* inputs */
+	: "memory"	/* clobbered regs */
+	);
+}
+
 #endif /* _k_main_h */