[kernel][thread] Add guard page around most kernel stacks

Allocate kernel stacks with vmm_alloc instead of malloc to get guard
pages.

The initial thread for each cpu, which later becomes idle threads, are
not affected by this, and will still run without guard pages.

Bug: 165825378
Change-Id: I103bcfdc330d88ac0957e3dad75aa232dc20e084
diff --git a/kernel/thread.c b/kernel/thread.c
index 4d1b05d..6b375e4 100644
--- a/kernel/thread.c
+++ b/kernel/thread.c
@@ -165,6 +165,7 @@
  */
 thread_t *thread_create_etc(thread_t *t, const char *name, thread_start_routine entry, void *arg, int priority, void *stack, size_t stack_size)
 {
+    int ret;
     unsigned int flags = 0;
 
     if (!t) {
@@ -197,8 +198,9 @@
         stack_size += THREAD_STACK_PADDING_SIZE;
         flags |= THREAD_FLAG_DEBUG_STACK_BOUNDS_CHECK;
 #endif
-        t->stack = malloc(stack_size);
-        if (!t->stack) {
+        ret = vmm_alloc(vmm_get_kernel_aspace(), "kernel-stack", stack_size,
+                        &t->stack, 0, 0, ARCH_MMU_FLAG_PERM_NO_EXECUTE);
+        if (ret) {
             if (flags & THREAD_FLAG_FREE_STRUCT)
                 free(t);
             return NULL;
@@ -384,7 +386,7 @@
 {
     /* free its stack and the thread structure itself */
     if (t->flags & THREAD_FLAG_FREE_STACK && t->stack)
-        free(t->stack);
+        vmm_free_region(vmm_get_kernel_aspace(), (vaddr_t)t->stack);
 
     if (t->flags & THREAD_FLAG_FREE_STRUCT)
         free(t);