kernel: add K_THREAD_STACK_RESERVED

This is used to have each arch canonically state how much
room in the stack object is reserved for non-thread use.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
diff --git a/include/arch/arc/arch.h b/include/arch/arc/arch.h
index 43cd6fa..2e2166f 100644
--- a/include/arch/arc/arch.h
+++ b/include/arch/arc/arch.h
@@ -81,6 +81,9 @@
 
 #if defined(CONFIG_USERSPACE)
 
+#define Z_ARCH_THREAD_STACK_RESERVED \
+	(STACK_GUARD_SIZE + CONFIG_PRIVILEGED_STACK_SIZE)
+
 #if CONFIG_ARC_MPU_VER == 2
 
 #define Z_ARCH_THREAD_STACK_DEFINE(sym, size) \
@@ -134,6 +137,8 @@
 
 #else /* CONFIG_USERSPACE */
 
+#define Z_ARCH_THREAD_STACK_RESERVED STACK_GUARD_SIZE
+
 #define Z_ARCH_THREAD_STACK_DEFINE(sym, size) \
 	struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) \
 		sym[size + STACK_GUARD_SIZE]
diff --git a/include/arch/arm/arch.h b/include/arch/arm/arch.h
index 725c351..5d3bfd4 100644
--- a/include/arch/arm/arch.h
+++ b/include/arch/arm/arch.h
@@ -122,6 +122,16 @@
 
 #if defined(CONFIG_USERSPACE) && \
 	defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT)
+/* Guard is 'carved-out' of the thread stack region, and the supervisor
+ * mode stack is allocated elsewhere by gen_priv_stack.py
+ */
+#define Z_ARCH_THREAD_STACK_RESERVED 0
+#else
+#define Z_ARCH_THREAD_STACK_RESERVED MPU_GUARD_ALIGN_AND_SIZE
+#endif
+
+#if defined(CONFIG_USERSPACE) && \
+	defined(CONFIG_MPU_REQUIRES_POWER_OF_TWO_ALIGNMENT)
 #define Z_ARCH_THREAD_STACK_DEFINE(sym, size) \
 	struct _k_thread_stack_element __noinit \
 		__aligned(POW2_CEIL(size)) sym[POW2_CEIL(size)]
diff --git a/include/arch/x86/arch.h b/include/arch/x86/arch.h
index c3b71ec..21bbf47 100644
--- a/include/arch/x86/arch.h
+++ b/include/arch/x86/arch.h
@@ -582,17 +582,17 @@
  * to the top of it.
  * All context switches will save/restore the esp0 field in the TSS.
  */
-#define _STACK_GUARD_SIZE	(MMU_PAGE_SIZE * 2)
+#define Z_ARCH_THREAD_STACK_RESERVED	(MMU_PAGE_SIZE * 2)
 #define _STACK_BASE_ALIGN	MMU_PAGE_SIZE
 #elif defined(CONFIG_HW_STACK_PROTECTION) || defined(CONFIG_USERSPACE)
 /* If only one of HW stack protection or userspace is enabled, then the
  * stack will be preceded by one page which is a guard page or a kernel mode
  * stack, respectively.
  */
-#define _STACK_GUARD_SIZE	MMU_PAGE_SIZE
+#define Z_ARCH_THREAD_STACK_RESERVED	MMU_PAGE_SIZE
 #define _STACK_BASE_ALIGN	MMU_PAGE_SIZE
 #else /* Neither feature */
-#define _STACK_GUARD_SIZE	0
+#define Z_ARCH_THREAD_STACK_RESERVED	0
 #define _STACK_BASE_ALIGN	STACK_ALIGN
 #endif
 
@@ -609,12 +609,12 @@
 #define Z_ARCH_THREAD_STACK_DEFINE(sym, size) \
 	struct _k_thread_stack_element __noinit \
 		__aligned(_STACK_BASE_ALIGN) \
-		sym[ROUND_UP((size), _STACK_SIZE_ALIGN) + _STACK_GUARD_SIZE]
+		sym[ROUND_UP((size), _STACK_SIZE_ALIGN) + Z_ARCH_THREAD_STACK_RESERVED]
 
 #define Z_ARCH_THREAD_STACK_LEN(size) \
 		(ROUND_UP((size), \
 			  MAX(_STACK_BASE_ALIGN, _STACK_SIZE_ALIGN)) + \
-		_STACK_GUARD_SIZE)
+		Z_ARCH_THREAD_STACK_RESERVED)
 
 #define Z_ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
 	struct _k_thread_stack_element __noinit \
@@ -623,13 +623,13 @@
 
 #define Z_ARCH_THREAD_STACK_MEMBER(sym, size) \
 	struct _k_thread_stack_element __aligned(_STACK_BASE_ALIGN) \
-		sym[ROUND_UP((size), _STACK_SIZE_ALIGN) + _STACK_GUARD_SIZE]
+		sym[ROUND_UP((size), _STACK_SIZE_ALIGN) + Z_ARCH_THREAD_STACK_RESERVED]
 
 #define Z_ARCH_THREAD_STACK_SIZEOF(sym) \
-	(sizeof(sym) - _STACK_GUARD_SIZE)
+	(sizeof(sym) - Z_ARCH_THREAD_STACK_RESERVED)
 
 #define Z_ARCH_THREAD_STACK_BUFFER(sym) \
-	((char *)((sym) + _STACK_GUARD_SIZE))
+	((char *)((sym) + Z_ARCH_THREAD_STACK_RESERVED))
 
 #if CONFIG_X86_KERNEL_OOPS
 #define Z_ARCH_EXCEPT(reason_p) do { \
diff --git a/include/kernel.h b/include/kernel.h
index c698e55..dde02ce 100644
--- a/include/kernel.h
+++ b/include/kernel.h
@@ -4580,6 +4580,7 @@
 #define K_THREAD_STACK_LEN(size) Z_ARCH_THREAD_STACK_LEN(size)
 #define K_THREAD_STACK_MEMBER(sym, size) Z_ARCH_THREAD_STACK_MEMBER(sym, size)
 #define K_THREAD_STACK_SIZEOF(sym) Z_ARCH_THREAD_STACK_SIZEOF(sym)
+#define K_THREAD_STACK_RESERVED Z_ARCH_THREAD_STACK_RESERVED
 static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
 {
 	return Z_ARCH_THREAD_STACK_BUFFER(sym);
@@ -4677,6 +4678,18 @@
  */
 #define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
 
+
+/**
+ * @brief Indicate how much additional memory is reserved for stack objects
+ *
+ * Any given stack declaration may have additional memory in it for guard
+ * areas or supervisor mode stacks. This macro indicates how much space
+ * is reserved for this. The memory reserved may not be contiguous within
+ * the stack object, and does not account for additional space used due to
+ * enforce alignment.
+ */
+#define K_THREAD_STACK_RESERVED		0
+
 /**
  * @brief Get a pointer to the physical stack buffer
  *