From 35f9062e37a7d6493b9b82da4da9440ec42a621a Mon Sep 17 00:00:00 2001
From: Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
Date: Wed, 15 May 2013 17:45:43 +0200
Subject: [PATCH] inital OSX support for native port

---
 Makefile.include              | 12 ++++++++++++
 cpu/native/hwtimer_cpu.c      | 16 ++++++++++++++++
 cpu/native/include/cpu-conf.h |  8 ++++++++
 cpu/native/include/cpu.h      |  6 ++++++
 cpu/native/irq_cpu.c          | 11 +++++++++--
 cpu/native/native_cpu.c       |  6 ++++++
 cpu/native/tramp.S            | 23 +++++++++++++++++++++++
 sys/lib/ringbuffer.c          |  7 ++++++-
 8 files changed, 86 insertions(+), 3 deletions(-)

diff --git a/Makefile.include b/Makefile.include
index 56cddaa8dc..c195d5bc21 100644
--- a/Makefile.include
+++ b/Makefile.include
@@ -21,14 +21,26 @@ BASELIBS += $(PROJBINDIR)/${PROJECT}.a
 
 PROJBINDIR =$(CURDIR)/bin
 
+# clumsy way to enable building native on osx:
+BUILDOSXNATIVE = 0
+ifeq ($(CPU),native)
+ifeq ($(shell uname -s),Darwin)
+	BUILDOSXNATIVE = 1
+endif
+endif
+
 ## make script for your project. Build RIOT-base here!
 all: $(PROJBINDIR)/$(PROJECT).a
 	@echo "Building project $(PROJECT) for $(BOARD) w/ MCU $(MCU)."
 	$(MAKE) -C $(RIOTBOARD)
 	$(MAKE) -C $(RIOTBASE)
+ifeq ($(BUILDOSXNATIVE),1)	
+	@$(LINK) $(UNDEF) -o $(PROJBINDIR)/$(PROJECT).elf $(BASELIBS) $(LINKFLAGS) -Wl,-no_pie
+else
 	@$(LINK) $(UNDEF) -o $(PROJBINDIR)/$(PROJECT).elf -Wl,--start-group $(BASELIBS) -lm -Wl,--end-group  -Wl,-Map=$(PROJBINDIR)/$(PROJECT).map $(LINKFLAGS) 
 	@$(SIZE) $(PROJBINDIR)/$(PROJECT).elf
 	@$(OBJCOPY) -O ihex $(PROJBINDIR)/$(PROJECT).elf $(PROJBINDIR)/$(PROJECT).hex
+endif
 
 ## your make rules
 ## Only basic example - modify it for larger projects!!
diff --git a/cpu/native/hwtimer_cpu.c b/cpu/native/hwtimer_cpu.c
index bda90b6b5e..db7b09ca34 100644
--- a/cpu/native/hwtimer_cpu.c
+++ b/cpu/native/hwtimer_cpu.c
@@ -20,6 +20,11 @@
  * @}
  */
 
+#ifdef __MACH__
+#include <mach/clock.h>
+#include <mach/mach.h>
+#endif
+
 #include <time.h>
 #include <sys/time.h>
 #include <signal.h>
@@ -204,9 +209,20 @@ unsigned long hwtimer_arch_now(void)
 
     DEBUG("hwtimer_arch_now()\n");
 
+#ifdef __MACH__ 
+    clock_serv_t cclock;
+    mach_timespec_t mts;
+    host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
+    clock_get_time(cclock, &mts);
+    mach_port_deallocate(mach_task_self(), cclock);
+    t.tv_sec = mts.tv_sec;
+    t.tv_nsec = mts.tv_nsec;
+#else
     if (clock_gettime(CLOCK_MONOTONIC, &t) == -1) {
         err(1, "hwtimer_arch_now: clock_gettime");
     }
+#endif
+
     native_hwtimer_now = ts2ticks(&t);
 
     DEBUG("hwtimer_arch_now(): it is now %lis %lins\n", t.tv_sec, t.tv_nsec);
diff --git a/cpu/native/include/cpu-conf.h b/cpu/native/include/cpu-conf.h
index 98be2377d9..2471a2b657 100644
--- a/cpu/native/include/cpu-conf.h
+++ b/cpu/native/include/cpu-conf.h
@@ -17,11 +17,19 @@
 #include <signal.h>
 
 /* TODO: choose more sensibly? */
+#ifdef __MACH__
+#define KERNEL_CONF_STACKSIZE_DEFAULT   (163840)
+#define KERNEL_CONF_STACKSIZE_IDLE      (163840)
+#define NATIVE_ISR_STACKSIZE            (163840)
+#define TRANSCEIVER_STACK_SIZE          (163840)
+#define MINIMUM_STACK_SIZE              (163840)
+#else
 #define KERNEL_CONF_STACKSIZE_DEFAULT   (16384)
 #define KERNEL_CONF_STACKSIZE_IDLE      (16384)
 #define NATIVE_ISR_STACKSIZE            (16384)
 #define TRANSCEIVER_STACK_SIZE          (16384)
 #define MINIMUM_STACK_SIZE              (16384)
+#endif
 
 /* for cc110x_ng */
 #define RX_BUF_SIZE (10)
diff --git a/cpu/native/include/cpu.h b/cpu/native/include/cpu.h
index ad29479cf8..2a51e9deef 100644
--- a/cpu/native/include/cpu.h
+++ b/cpu/native/include/cpu.h
@@ -19,7 +19,13 @@
 #ifndef _CPU_H
 #define _CPU_H
 
+#ifdef __MACH__
+#define _XOPEN_SOURCE
+#endif
 #include <ucontext.h>
+#ifdef __MACH__
+#undef _XOPEN_SOURCE
+#endif
 
 #include "kernel_intern.h"
 #include "sched.h"
diff --git a/cpu/native/irq_cpu.c b/cpu/native/irq_cpu.c
index 70bc44054e..2da242cb97 100644
--- a/cpu/native/irq_cpu.c
+++ b/cpu/native/irq_cpu.c
@@ -48,6 +48,8 @@ struct int_handler_t {
 static struct int_handler_t native_irq_handlers[255];
 char sigalt_stk[SIGSTKSZ];
 
+#define SIGMAX (255) // XXX: do this properly if possible
+
 void print_thread_sigmask(ucontext_t *cp)
 {
     sigset_t *p = &cp->uc_sigmask;
@@ -55,7 +57,7 @@ void print_thread_sigmask(ucontext_t *cp)
         err(1, "print_thread_sigmask: sigemptyset");
     }
 
-    for (int i = 1; i<(SIGRTMAX); i++) {
+    for (int i = 1; i<(SIGMAX); i++) {
         if (native_irq_handlers[i].func != NULL) {
             printf("%s: %s\n",
                     strsignal(i),
@@ -98,7 +100,7 @@ void native_print_signals()
         err(1, "native_print_signals(): sigprocmask");
     }
 
-    for (int i = 1; i<(SIGRTMAX); i++) {
+    for (int i = 1; i<(SIGMAX); i++) {
         if (native_irq_handlers[i].func != NULL || i == SIGUSR1) {
             printf("%s: %s\n",
                     strsignal(i),
@@ -291,8 +293,13 @@ void native_isr_entry(int sig, siginfo_t *info, void *context)
     if (_native_in_syscall == 0) {
         _native_in_isr = 1;
         DEBUG("\n\n\t\treturn to _native_sig_leave_tramp\n\n");
+#ifdef __MACH__
+        _native_saved_eip =  ((ucontext_t*)context)->uc_mcontext->__ss.__eip;
+        ((ucontext_t*)context)->uc_mcontext->__ss.__eip = (unsigned int)&_native_sig_leave_tramp;
+#else
         _native_saved_eip = ((ucontext_t*)context)->uc_mcontext.gregs[REG_EIP];
         ((ucontext_t*)context)->uc_mcontext.gregs[REG_EIP] = (unsigned int)&_native_sig_leave_tramp;
+#endif
         // TODO: change sigmask?
     }
     else {
diff --git a/cpu/native/native_cpu.c b/cpu/native/native_cpu.c
index 89337e7495..369a988fcb 100644
--- a/cpu/native/native_cpu.c
+++ b/cpu/native/native_cpu.c
@@ -15,7 +15,13 @@
  * @author  Ludwig Ortmann <ludwig.ortmann@fu-berlin.de>
  */
 #include <stdio.h>
+#ifdef __MACH__
+#define _XOPEN_SOURCE
+#endif
 #include <ucontext.h>
+#ifdef __MACH__
+#undef _XOPEN_SOURCE
+#endif
 #include <err.h>
 
 #include "kernel_intern.h"
diff --git a/cpu/native/tramp.S b/cpu/native/tramp.S
index 427f485ff1..4ba4b5d536 100644
--- a/cpu/native/tramp.S
+++ b/cpu/native/tramp.S
@@ -1,5 +1,27 @@
 .text
 
+#ifdef __MACH__
+.globl __native_sig_leave_tramp
+__native_sig_leave_tramp:
+    pushl %eax
+    pushf
+    pushl %ebp
+    pushl %esp
+
+    movl  %esp, %ebp
+    subl  $24, %esp
+    movl  $__native_isr_ctx, 4(%esp)
+    movl  $__native_cur_ctx, (%esp)
+    call _swapcontext
+
+    addl  $24, %esp
+    popl %esp
+    popl %ebp
+    popf 
+    popl %eax
+
+    jmp *__native_saved_eip
+#else
 .extern $_native_saved_eip
 .extern $_native_isr_ctx
 .extern $_native_cur_ctx
@@ -27,3 +49,4 @@ _native_sig_leave_tramp:
     movl $0x0, _native_in_isr;
 
     jmp *_native_saved_eip
+#endif
diff --git a/sys/lib/ringbuffer.c b/sys/lib/ringbuffer.c
index 4e021942d5..3d6083f803 100755
--- a/sys/lib/ringbuffer.c
+++ b/sys/lib/ringbuffer.c
@@ -1,8 +1,13 @@
 #include <stdio.h>
 #include <stdint.h>
-#include <malloc.h>
 #include <string.h>
 
+#ifdef __MACH__
+#include <stdlib.h>
+#else
+#include "malloc.h"
+#endif
+
 #include "ringbuffer.h"
 
 //#define DEBUG(...) printf (__VA_ARGS__)
-- 
GitLab