kernel: Add kswap.h header to unbreak cycles

The xtensa-asm2 work included a patch that added nano_internal.h
includes in lots of places that needed to have _Swap defined, because
it had to break a cycle and this no longer got pulled in from the arch
headers.

Unfortunately those new includes created new and more amusing cycles
elsewhere which led to breakage on other platforms.

Break out the _Swap definition (only) into a separate header and use
that instead.  Cleaner.  Seems not to have any more hidden gotchas.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
diff --git a/arch/arm/core/thread_abort.c b/arch/arm/core/thread_abort.c
index 3d398a5..f1673af 100644
--- a/arch/arm/core/thread_abort.c
+++ b/arch/arm/core/thread_abort.c
@@ -21,6 +21,7 @@
 #include <toolchain.h>
 #include <linker/sections.h>
 #include <ksched.h>
+#include <kswap.h>
 #include <wait_q.h>
 #include <misc/__assert.h>
 
diff --git a/arch/posix/core/posix_core.c b/arch/posix/core/posix_core.c
index e3a8021..a72100f 100644
--- a/arch/posix/core/posix_core.c
+++ b/arch/posix/core/posix_core.c
@@ -48,6 +48,7 @@
 #include "kernel_internal.h"
 #include "kernel_structs.h"
 #include "ksched.h"
+#include "kswap.h"
 
 #define PREFIX     "POSIX arch core: "
 #define ERPREFIX   PREFIX"error on "
diff --git a/arch/x86/core/irq_manage.c b/arch/x86/core/irq_manage.c
index 4fee223..388340c 100644
--- a/arch/x86/core/irq_manage.c
+++ b/arch/x86/core/irq_manage.c
@@ -21,6 +21,7 @@
 #include <misc/printk.h>
 #include <irq.h>
 #include <logging/kernel_event_logger.h>
+#include <kswap.h>
 
 extern void _SpuriousIntHandler(void *);
 extern void _SpuriousIntNoErrCodeHandler(void *);
diff --git a/arch/xtensa/core/xtensa-asm2.c b/arch/xtensa/core/xtensa-asm2.c
index f5bbdd8..05a3617 100644
--- a/arch/xtensa/core/xtensa-asm2.c
+++ b/arch/xtensa/core/xtensa-asm2.c
@@ -10,6 +10,7 @@
 #include <ksched.h>
 #include <kernel_structs.h>
 #include <nano_internal.h>
+#include <kswap.h>
 #include <_soc_inthandlers.h>
 
 void *xtensa_init_stack(int *stack_top,
diff --git a/boards/posix/native_posix/irq_handler.c b/boards/posix/native_posix/irq_handler.c
index f443fb1..8723c14 100644
--- a/boards/posix/native_posix/irq_handler.c
+++ b/boards/posix/native_posix/irq_handler.c
@@ -12,6 +12,7 @@
 #include "irq_offload.h"
 #include "kernel_structs.h"
 #include "kernel_internal.h"
+#include "kswap.h"
 #include "irq_ctrl.h"
 #include "posix_core.h"
 #include "board_soc.h"
diff --git a/kernel/include/kernel_internal.h b/kernel/include/kernel_internal.h
index 47febca..2a3e986 100644
--- a/kernel/include/kernel_internal.h
+++ b/kernel/include/kernel_internal.h
@@ -15,8 +15,6 @@
 #define _NANO_INTERNAL__H_
 
 #include <kernel.h>
-#include <kernel_structs.h>
-#include <ksched.h>
 
 #ifndef _ASMLANGUAGE
 
@@ -52,65 +50,6 @@
 			      void *p1, void *p2, void *p3,
 			      int prio, u32_t options);
 
-#ifdef CONFIG_TIMESLICING
-extern void _update_time_slice_before_swap(void);
-#else
-#define _update_time_slice_before_swap() /**/
-#endif
-
-#ifdef CONFIG_STACK_SENTINEL
-extern void _check_stack_sentinel(void);
-#else
-#define _check_stack_sentinel() /**/
-#endif
-
-/* context switching and scheduling-related routines */
-#ifdef CONFIG_USE_SWITCH
-
-/* New style context switching.  _arch_switch() is a lower level
- * primitive that doesn't know about the scheduler or return value.
- * Needed for SMP, where the scheduler requires spinlocking that we
- * don't want to have to do in per-architecture assembly.
- */
-static inline unsigned int _Swap(unsigned int key)
-{
-	struct k_thread *new_thread, *old_thread;
-	int ret;
-
-	old_thread = _kernel.current;
-
-	_check_stack_sentinel();
-	_update_time_slice_before_swap();
-
-	new_thread = _get_next_ready_thread();
-
-	old_thread->swap_retval = -EAGAIN;
-
-	_kernel.current = new_thread;
-	_arch_switch(new_thread->switch_handle,
-		     &old_thread->switch_handle);
-
-	ret = _kernel.current->swap_retval;
-
-	irq_unlock(key);
-
-	return ret;
-}
-
-#else /* !CONFIG_USE_SWITCH */
-
-extern unsigned int __swap(unsigned int key);
-
-static inline unsigned int _Swap(unsigned int key)
-{
-	_check_stack_sentinel();
-	_update_time_slice_before_swap();
-
-	return __swap(key);
-}
-#endif
-
-
 #ifdef CONFIG_USERSPACE
 /**
  * @brief Get the maximum number of partitions for a memory domain
diff --git a/kernel/include/kswap.h b/kernel/include/kswap.h
new file mode 100644
index 0000000..a8be616
--- /dev/null
+++ b/kernel/include/kswap.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2018 Intel Corporation
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+#ifndef _KSWAP_H
+#define _KSWAP_H
+
+#include <ksched.h>
+#include <kernel_arch_func.h>
+
+#ifdef CONFIG_TIMESLICING
+extern void _update_time_slice_before_swap(void);
+#else
+#define _update_time_slice_before_swap() /**/
+#endif
+
+#ifdef CONFIG_STACK_SENTINEL
+extern void _check_stack_sentinel(void);
+#else
+#define _check_stack_sentinel() /**/
+#endif
+
+/* context switching and scheduling-related routines */
+#ifdef CONFIG_USE_SWITCH
+
+/* New style context switching.  _arch_switch() is a lower level
+ * primitive that doesn't know about the scheduler or return value.
+ * Needed for SMP, where the scheduler requires spinlocking that we
+ * don't want to have to do in per-architecture assembly.
+ */
+static inline unsigned int _Swap(unsigned int key)
+{
+	struct k_thread *new_thread, *old_thread;
+	int ret;
+
+	old_thread = _kernel.current;
+
+	_check_stack_sentinel();
+	_update_time_slice_before_swap();
+
+	new_thread = _get_next_ready_thread();
+
+	old_thread->swap_retval = -EAGAIN;
+
+	_kernel.current = new_thread;
+	_arch_switch(new_thread->switch_handle,
+		     &old_thread->switch_handle);
+
+	ret =_kernel.current->swap_retval;
+
+	irq_unlock(key);
+
+	return ret;
+}
+
+#else /* !CONFIG_USE_SWITCH */
+
+extern unsigned int __swap(unsigned int key);
+
+static inline unsigned int _Swap(unsigned int key)
+{
+	_check_stack_sentinel();
+	_update_time_slice_before_swap();
+
+	return __swap(key);
+}
+#endif
+
+#endif /* _KSWAP_H */
diff --git a/kernel/init.c b/kernel/init.c
index 43b71e4..cbdf5b4 100644
--- a/kernel/init.c
+++ b/kernel/init.c
@@ -28,6 +28,7 @@
 #include <string.h>
 #include <misc/dlist.h>
 #include <nano_internal.h>
+#include <kswap.h>
 
 /* kernel build timestamp items */
 
diff --git a/kernel/mailbox.c b/kernel/mailbox.c
index 42dcd7c..03592eb 100644
--- a/kernel/mailbox.c
+++ b/kernel/mailbox.c
@@ -17,7 +17,7 @@
 #include <wait_q.h>
 #include <misc/dlist.h>
 #include <init.h>
-#include <nano_internal.h>
+#include <kswap.h>
 
 #if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
 
diff --git a/kernel/mem_slab.c b/kernel/mem_slab.c
index 6a4f493..63d3507 100644
--- a/kernel/mem_slab.c
+++ b/kernel/mem_slab.c
@@ -13,7 +13,7 @@
 #include <misc/dlist.h>
 #include <ksched.h>
 #include <init.h>
-#include <nano_internal.h>
+#include <kswap.h>
 
 extern struct k_mem_slab _k_mem_slab_list_start[];
 extern struct k_mem_slab _k_mem_slab_list_end[];
diff --git a/kernel/mempool.c b/kernel/mempool.c
index 372cdef..7e97911 100644
--- a/kernel/mempool.c
+++ b/kernel/mempool.c
@@ -10,7 +10,7 @@
 #include <init.h>
 #include <string.h>
 #include <misc/__assert.h>
-#include <nano_internal.h>
+#include <kswap.h>
 
 /* Linker-defined symbols bound the static pool structs */
 extern struct k_mem_pool _k_mem_pool_list_start[];
diff --git a/kernel/msg_q.c b/kernel/msg_q.c
index e9b7a69..9e8f7e2 100644
--- a/kernel/msg_q.c
+++ b/kernel/msg_q.c
@@ -20,7 +20,7 @@
 #include <misc/dlist.h>
 #include <init.h>
 #include <syscall_handler.h>
-#include <nano_internal.h>
+#include <kswap.h>
 
 extern struct k_msgq _k_msgq_list_start[];
 extern struct k_msgq _k_msgq_list_end[];
diff --git a/kernel/mutex.c b/kernel/mutex.c
index 22b9ff9..3f156c6 100644
--- a/kernel/mutex.c
+++ b/kernel/mutex.c
@@ -36,7 +36,7 @@
 #include <errno.h>
 #include <init.h>
 #include <syscall_handler.h>
-#include <nano_internal.h>
+#include <kswap.h>
 
 #define RECORD_STATE_CHANGE(mutex) do { } while ((0))
 #define RECORD_CONFLICT(mutex) do { } while ((0))
diff --git a/kernel/pipes.c b/kernel/pipes.c
index b1eb1f4..f8f29da 100644
--- a/kernel/pipes.c
+++ b/kernel/pipes.c
@@ -19,7 +19,7 @@
 #include <misc/dlist.h>
 #include <init.h>
 #include <syscall_handler.h>
-#include <nano_internal.h>
+#include <kswap.h>
 
 struct k_pipe_desc {
 	unsigned char *buffer;           /* Position in src/dest buffer */
diff --git a/kernel/poll.c b/kernel/poll.c
index 8066063..d5c7996 100644
--- a/kernel/poll.c
+++ b/kernel/poll.c
@@ -19,6 +19,7 @@
 #include <nano_internal.h>
 #include <wait_q.h>
 #include <ksched.h>
+#include <kswap.h>
 #include <misc/slist.h>
 #include <misc/dlist.h>
 #include <misc/__assert.h>
diff --git a/kernel/posix/pthread_barrier.c b/kernel/posix/pthread_barrier.c
index f4966af..445eb32 100644
--- a/kernel/posix/pthread_barrier.c
+++ b/kernel/posix/pthread_barrier.c
@@ -8,7 +8,7 @@
 #include <pthread.h>
 #include "ksched.h"
 #include "wait_q.h"
-#include <nano_internal.h>
+#include <kswap.h>
 
 void ready_one_thread(_wait_q_t *wq);
 
diff --git a/kernel/posix/pthread_cond.c b/kernel/posix/pthread_cond.c
index 48f1a16..d4d6499 100644
--- a/kernel/posix/pthread_cond.c
+++ b/kernel/posix/pthread_cond.c
@@ -8,7 +8,7 @@
 #include <pthread.h>
 #include "ksched.h"
 #include "wait_q.h"
-#include <nano_internal.h>
+#include <kswap.h>
 
 void ready_one_thread(_wait_q_t *wq);
 
diff --git a/kernel/queue.c b/kernel/queue.c
index ab3b2c6..ce566d9 100644
--- a/kernel/queue.c
+++ b/kernel/queue.c
@@ -20,7 +20,7 @@
 #include <ksched.h>
 #include <misc/slist.h>
 #include <init.h>
-#include <nano_internal.h>
+#include <kswap.h>
 
 extern struct k_queue _k_queue_list_start[];
 extern struct k_queue _k_queue_list_end[];
diff --git a/kernel/sched.c b/kernel/sched.c
index 9e2dc56..c93e2d0 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -11,7 +11,7 @@
 #include <wait_q.h>
 #include <misc/util.h>
 #include <syscall_handler.h>
-#include <nano_internal.h>
+#include <kswap.h>
 
 /* the only struct _kernel instance */
 struct _kernel _kernel = {0};
diff --git a/kernel/sem.c b/kernel/sem.c
index 85d3b94..39bc5c6 100644
--- a/kernel/sem.c
+++ b/kernel/sem.c
@@ -27,7 +27,7 @@
 #include <ksched.h>
 #include <init.h>
 #include <syscall_handler.h>
-#include <nano_internal.h>
+#include <kswap.h>
 
 extern struct k_sem _k_sem_list_start[];
 extern struct k_sem _k_sem_list_end[];
diff --git a/kernel/stack.c b/kernel/stack.c
index d6fbdd1..1d0c608 100644
--- a/kernel/stack.c
+++ b/kernel/stack.c
@@ -18,7 +18,7 @@
 #include <misc/__assert.h>
 #include <init.h>
 #include <syscall_handler.h>
-#include <nano_internal.h>
+#include <kswap.h>
 
 extern struct k_stack _k_stack_list_start[];
 extern struct k_stack _k_stack_list_end[];
diff --git a/kernel/thread.c b/kernel/thread.c
index 8f4c148..2ef739b 100644
--- a/kernel/thread.c
+++ b/kernel/thread.c
@@ -25,6 +25,7 @@
 #include <atomic.h>
 #include <syscall_handler.h>
 #include <nano_internal.h>
+#include <kswap.h>
 
 extern struct _static_thread_data _static_thread_data_list_start[];
 extern struct _static_thread_data _static_thread_data_list_end[];
diff --git a/kernel/thread_abort.c b/kernel/thread_abort.c
index ea87bc1..5cd3ac7 100644
--- a/kernel/thread_abort.c
+++ b/kernel/thread_abort.c
@@ -13,6 +13,8 @@
 #include <kernel.h>
 #include <kernel_structs.h>
 #include <kernel_internal.h>
+#include <nano_internal.h>
+#include <kswap.h>
 #include <string.h>
 #include <toolchain.h>
 #include <linker/sections.h>
diff --git a/kernel/timer.c b/kernel/timer.c
index 840b3cc..e4e5526 100644
--- a/kernel/timer.c
+++ b/kernel/timer.c
@@ -9,7 +9,7 @@
 #include <init.h>
 #include <wait_q.h>
 #include <syscall_handler.h>
-#include <nano_internal.h>
+#include <kswap.h>
 
 extern struct k_timer _k_timer_list_start[];
 extern struct k_timer _k_timer_list_end[];
diff --git a/tests/kernel/fatal/src/main.c b/tests/kernel/fatal/src/main.c
index 135c678..49027ca 100644
--- a/tests/kernel/fatal/src/main.c
+++ b/tests/kernel/fatal/src/main.c
@@ -9,7 +9,7 @@
 #include <tc_util.h>
 #include <kernel_structs.h>
 #include <irq_offload.h>
-#include <nano_internal.h>
+#include <kswap.h>
 
 #if defined(CONFIG_X86) && defined(CONFIG_X86_MMU)
 #define STACKSIZE (8192)