kernel: expose k_busy_wait() to user mode

If we just had the kernel's implementation, we could
just move this to lib/, but possible arch-specific
implementations dictate that we just make this a
syscall.

Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
diff --git a/boards/posix/nrf52_bsim/k_busy_wait.c b/boards/posix/nrf52_bsim/k_busy_wait.c
index ba7db49..8e43ed7 100644
--- a/boards/posix/nrf52_bsim/k_busy_wait.c
+++ b/boards/posix/nrf52_bsim/k_busy_wait.c
@@ -14,9 +14,9 @@
  * Will block this thread (and therefore the whole zephyr) during usec_to_wait
  *
  * Note that interrupts may be received in the meanwhile and that therefore this
- * thread may loose context
+ * thread may lose context
  */
-void k_busy_wait(u32_t usec_to_wait)
+void z_arch_busy_wait(u32_t usec_to_wait)
 {
 	bs_time_t time_end = tm_get_hw_time() + usec_to_wait;
 
diff --git a/drivers/timer/native_posix_timer.c b/drivers/timer/native_posix_timer.c
index 7ac1d85..b7f0a30 100644
--- a/drivers/timer/native_posix_timer.c
+++ b/drivers/timer/native_posix_timer.c
@@ -125,7 +125,7 @@
  * Note that interrupts may be received in the meanwhile and that therefore this
  * thread may loose context
  */
-void k_busy_wait(u32_t usec_to_wait)
+void z_arch_busy_wait(u32_t usec_to_wait)
 {
 	u64_t time_end = hwm_get_time() + usec_to_wait;
 
diff --git a/include/kernel.h b/include/kernel.h
index 43dfd36..f859784 100644
--- a/include/kernel.h
+++ b/include/kernel.h
@@ -800,7 +800,7 @@
  *
  * @return N/A
  */
-extern void k_busy_wait(u32_t usec_to_wait);
+__syscall void k_busy_wait(u32_t usec_to_wait);
 
 /**
  * @brief Yield the current thread.
diff --git a/kernel/include/kernel_internal.h b/kernel/include/kernel_internal.h
index 3e239c4..6ab7632 100644
--- a/kernel/include/kernel_internal.h
+++ b/kernel/include/kernel_internal.h
@@ -227,6 +227,10 @@
 extern int z_stack_adjust_initialized;
 #endif
 
+#if defined(CONFIG_ARCH_HAS_CUSTOM_BUSY_WAIT)
+extern void z_arch_busy_wait(u32_t usec_to_wait);
+#endif
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/kernel/thread.c b/kernel/thread.c
index 4417cbb..e035c24 100644
--- a/kernel/thread.c
+++ b/kernel/thread.c
@@ -94,9 +94,10 @@
 	return _current->base.user_options & K_ESSENTIAL;
 }
 
-#if !defined(CONFIG_ARCH_HAS_CUSTOM_BUSY_WAIT)
-void k_busy_wait(u32_t usec_to_wait)
+#ifdef CONFIG_SYS_CLOCK_EXISTS
+void _impl_k_busy_wait(u32_t usec_to_wait)
 {
+#if !defined(CONFIG_ARCH_HAS_CUSTOM_BUSY_WAIT)
 	/* use 64-bit math to prevent overflow when multiplying */
 	u32_t cycles_to_wait = (u32_t)(
 		(u64_t)usec_to_wait *
@@ -113,8 +114,19 @@
 			break;
 		}
 	}
+#else
+	z_arch_busy_wait(usec_to_wait);
+#endif /* CONFIG_ARCH_HAS_CUSTOM_BUSY_WAIT */
 }
-#endif
+
+#ifdef CONFIG_USERSPACE
+Z_SYSCALL_HANDLER(k_busy_wait, usec_to_wait)
+{
+	_impl_k_busy_wait(usec_to_wait);
+	return 0;
+}
+#endif /* CONFIG_USERSPACE */
+#endif /* CONFIG_SYS_CLOCK_EXISTS */
 
 #ifdef CONFIG_THREAD_CUSTOM_DATA
 void _impl_k_thread_custom_data_set(void *value)
diff --git a/soc/posix/inf_clock/posix_board_if.h b/soc/posix/inf_clock/posix_board_if.h
index 3ebd2f7..2d47e7b 100644
--- a/soc/posix/inf_clock/posix_board_if.h
+++ b/soc/posix/inf_clock/posix_board_if.h
@@ -25,10 +25,6 @@
 void posix_exit(int exit_code);
 u64_t posix_get_hw_cycle(void);
 
-#if defined(CONFIG_ARCH_HAS_CUSTOM_BUSY_WAIT)
-void k_busy_wait(u32_t usec_to_wait);
-#endif
-
 #ifdef __cplusplus
 }
 #endif