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

[MiNT] patch:XaAES:stackcheck



I've added a check for stack-alignment to XaAES.

---------------------------------------------------------------------------
This patch enables to detect and display the alignment of the
stackpointer in all three threads of XaAES. If one is not on a
word-boundary, an alert is issued. This patch should be removed
later.

Some minor optimizations are also included.
---------------------------------------------------------------------------


--- src.km/init.c	14 Jan 2010 21:54:54 -0000	1.79.2.3
+++ src.km/init.c	3 Feb 2010 23:05:17 -0000
@@ -51,6 +51,7 @@
 
 //long module_exit(void);
 
+short check_stack_alignment( long e );
 
 short my_global_aes[16];
 
@@ -297,6 +298,23 @@
 	return false;
 }
 #endif
+
+/*
+ * return alignment-value
+ *
+ * do not inline!
+ */
+short check_stack_alignment( long stk )
+{
+	/* Read the current stack pointer value */
+
+	if( stk & 1L )
+		return 1;
+	if( stk & 2L )
+		return 2;
+	return 4;
+}
+
 #define XA_SEM 0x58414553L	/*"XAES"*/
 #define SEMCREATE	0
 #define SEMDESTROY	1
@@ -328,12 +346,14 @@
  * - setup internal data
  * - start main kernel thread
  */
+unsigned short stack_align = 0;
 static Path start_path;
 static const struct kernel_module *self = NULL;
 
 long
 init(struct kentry *k, const struct kernel_module *km) //const char *path)
 {
+	long stk = (long)get_sp();
 	long err = 0L;
 
 	bool first = true;
@@ -381,6 +401,19 @@
 	else
 		BLOG((false, "\n~~~~~~~~~~~~ XaAES restarting! ~~~~~~~~~~~~~~~"));
 #endif
+
+	/**** check if stack is sane ****/
+	stack_align |= check_stack_alignment(stk);
+	if( stack_align == 1 )
+	{
+		BLOG(( 0,"WARNING: stack is odd:%lx!", stk ));
+	}
+	else if( stack_align == 4 )
+			BLOG(( 0,"stack is long-aligned:%lx", stk ));
+	else
+			BLOG(( 0,"stack is word-aligned:%lx", stk ));
+
+
 	if (check_kentry_version())
 	{
 		err = ENOSYS;
@@ -416,8 +449,9 @@
 
 		if (flag)
 		{
-			BLOG((/*00000005*/true, "ERROR: There exist an moose.xdd in your FreeMiNT sysdir."));
-			BLOG((/*00000006*/true, "       Please remove it before starting the XaAES kernel module!"));
+			BLOG((true,
+"ERROR: There exist an moose.xdd in your FreeMiNT sysdir.\n"
+"       Please remove it before starting the XaAES kernel module!"));
 			err = EINVAL;
 			goto error;
 		}
@@ -431,10 +465,12 @@
 
 		if (flag)
 		{
-			BLOG((true, "ERROR: There exist an moose.adi in your FreeMiNT sysdir."));
-			BLOG((true, " sysdir = '%s'", sysdir));
-			BLOG((true, "       Please remove it and install it in the XaAES module directory"));
-			BLOG((true, "       before starting the XaAES kernel module!"));
+			BLOG((true,
+"ERROR: There exists a moose.adi in your FreeMiNT sysdir.\n"
+" sysdir = '%s'\n"
+"       Please remove it and install it in the XaAES module directory\n"
+"       before starting the XaAES kernel module!\n",
+			sysdir));
 			err = EINVAL;
 			goto error;
 		}
@@ -589,9 +625,11 @@
 		if (!flag)
 		{
 
-			BLOG((/*00000008*/true, "ERROR: There exist no moose.adi in your XaAES module directory."));
-			BLOG((true, " -> '%s'", C.Aes->home_path));
-			BLOG((/*00000009*/true, "       Please install it before starting the XaAES kernel module!"));
+			BLOG((true,
+"ERROR: There exist no moose.adi in your XaAES module directory.\n"
+"	  -> '%s'"
+"   Please install it before starting the XaAES kernel module!",
+				C.Aes->home_path));
 			err = EINVAL;
 			goto error;
 		}
@@ -620,7 +658,7 @@
 		helper = (s_system(S_GETBVAL, 0x0484, 0)) | 8;
 		s_system(S_SETBVAL, 0x0484, (char)helper);
 	}
-	BLOG((false, "set bit 3 in conterm ok!"));
+//	BLOG((false, "set bit 3 in conterm ok!"));
 
 #if GENERATE_DIAGS
 	{ short nkc_vers = nkc_init(); DIAGS(("nkc_init: version %x", nkc_vers)); }
@@ -692,7 +730,6 @@
 
 		while (!(C.shutdown & QUIT_NOW))
 		{
-			BLOG((0,"init:sleep:%lx", &loader_pid));
 			sleep(WAIT_Q, (long)&loader_pid);
 		}
 
@@ -728,22 +765,22 @@
 	detach_extension((void *)-1L, XAAES_MAGIC);
 	detach_extension((void *)-1L, XAAES_MAGIC_SH);
 
+error:
 	/* delete semaphore */
 	{
-		int e = p_semaphore( SEMDESTROY, XA_SEM, 0 );
-		if( e )
-			BLOG((0,"init:could not destroy semaphore:%d", e ));
+		int r = p_semaphore( SEMDESTROY, XA_SEM, 0 );
+		if( r )
+			BLOG((0,"init:could not destroy semaphore:%d", r ));
 	}
-	BLOG((0,"init:return 0"));
 	/* succeeded */
-	return 0;
+	//return 0;
 
-error:
 #if GENERATE_DIAGS
 	/* Close the debug output file */
 	if (D.debug_file)
 		kernel_close(D.debug_file);
 #endif
+	BLOG((0,"init:return %ld", err));
 	return err;
 }
 
--- src.km/k_main.c	13 Jan 2010 22:21:06 -0000	1.140.2.2
+++ src.km/k_main.c	3 Feb 2010 23:05:18 -0000
@@ -62,6 +62,7 @@
 #include "mint/ssystem.h"
 #include "cookie.h"
 
+short check_stack_alignment( long e );
 
 #include "c_mouse.h"
 void set_tty_mode( short md );
@@ -947,9 +948,9 @@
 }
 #endif
 
+extern char XAAESNAME[];
 #if ALERT_SHUTDOWN
 int xaaes_do_form_alert( enum locks lock, int def_butt, char al_text[], char title[] );
-extern char XAAESNAME[];
 extern char ASK_SHUTDOWN_ALERT[];
 #endif
 
@@ -1012,6 +1013,9 @@
 	if (!client->tp_term)
 		do_block(client);
 }
+
+extern short stack_align;
+
 /*
  * AES thread
  */
@@ -1037,8 +1041,11 @@
 static void
 helpthread_entry(void *c)
 {
+	long stk = (long)get_sp();
 	struct xa_client *client;
 
+	stack_align |= (check_stack_alignment(stk) << 8);
+
 	p_domain(1);
 	setup_common();
 
@@ -1432,10 +1439,12 @@
 void
 k_main(void *dummy)
 {
+	long stk = (long)get_sp();
 	int wait = 1;
 	unsigned long default_input_channels;
 	struct tty *tty;
-	//long n = 0;
+
+	stack_align |= (check_stack_alignment(stk) << 4);
 
 	/*
 	 * setup kernel thread
@@ -1638,6 +1647,11 @@
 	if (cfg.opentaskman)
 		post_cevent(C.Hlp, ceExecfunc, open_taskmanager,NULL, 1,0, NULL,NULL);
 
+	if( stack_align & 0x111 )
+	{
+		ALERT(( "WARNING:your stack is odd!" ));
+	}
+
 	post_cevent(C.Hlp, CE_start_apps, NULL,NULL, 0,0, NULL,NULL);
 
 	set_tty_mode( COOKED );
@@ -1871,17 +1886,8 @@
 
 	if (C.alert_pipe > 0)
 		f_close(C.alert_pipe);
-#if 0
-	{
-	struct proc *lp = pid2proc( loader_pid );
-	BLOG((false, "Waking up loader: pid=%ld:%lx wait_cond=%lx wait_q=%d", loader_pid, &loader_pid, lp->wait_cond, lp->wait_q));
-	/* wakeup loader */
-
-	wake(WAIT_Q, lp->wait_cond );
-	}
-#else
 	wake(WAIT_Q, (long)&loader_pid);
-#endif
+
 	/* XXX todo -> module_exit */
 // 	display("kthread_exit...");
 
--- src.km/k_main.h	4 Jan 2010 22:31:11 -0000	1.24.2.1
+++ src.km/k_main.h	3 Feb 2010 23:05:18 -0000
@@ -67,4 +67,11 @@
 
 extern int aessys_timeout;
 
+/* Read the current stack pointer value */
+static __inline__ void* get_sp(void)
+{
+     register void* ret __asm__("sp");
+     return ret;
+}
+
 #endif /* _k_main_h */