Skip to content

Commit

Permalink
Merge pull request #1022 from LudwigOrtmann/issue_505
Browse files Browse the repository at this point in the history
native: update support for FreeBSD
  • Loading branch information
Kijewski committed Apr 26, 2014
2 parents 459599e + c2b9d94 commit 4c66f72
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 29 deletions.
23 changes: 18 additions & 5 deletions boards/native/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export ELF = $(BINDIR)$(PROJECT).elf
# toolchain:
export PREFIX =
export CC ?= $(PREFIX)gcc
export AR = $(PREFIX)ar
export AS = $(PREFIX)as
export LINK = $(PREFIX)gcc
export SIZE = $(PREFIX)size
export AR ?= $(PREFIX)ar
export AS ?= $(PREFIX)as
export LINK ?= $(PREFIX)gcc
export SIZE ?= $(PREFIX)size
export OBJCOPY = true

export DEBUGGER = gdb
Expand All @@ -25,7 +25,20 @@ export GPROF ?= gprof

# flags:
export CFLAGS += -Wall -Wextra -pedantic -m32
export LINKFLAGS += -m32 -gc -ldl
ifeq ($(shell uname -s),FreeBSD)
ifeq ($(shell uname -m),amd64)
export CFLAGS += -DCOMPAT_32BIT -L/usr/lib32 -B/usr/lib32
endif
endif
export LINKFLAGS += -m32 -gc
ifeq ($(shell uname -s),FreeBSD)
ifeq ($(shell uname -m),amd64)
export LINKFLAGS += -DCOMPAT_32BIT -L/usr/lib32 -B/usr/lib32
endif
export LINKFLAGS += -L $(BINDIR)
else
export LINKFLAGS += -ldl
endif
export ASFLAGS =
export DEBUGGER_FLAGS = $(ELF)
term-memcheck: export VALGRIND_FLAGS ?= --track-origins=yes
Expand Down
44 changes: 22 additions & 22 deletions cpu/native/include/native_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@
#define _NATIVE_INTERNAL_H

#include <signal.h>
/* enable signal handler register access on different platforms
* check here for more:
* http://sourceforge.net/p/predef/wiki/OperatingSystems/
*/
#if (defined(__FreeBSD__) || defined(__MACH__))
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#include <ucontext.h>
#undef _XOPEN_SOURCE
#else
#include <ucontext.h>
#endif
#elif defined(__linux__)
#ifndef _GNU_SOURCE
#define GNU_SOURCE
#include <ucontext.h>
#undef GNU_SOURCE
#else
#include <ucontext.h>
#endif
#endif // BSD/Linux


/**
* internal functions
Expand Down Expand Up @@ -76,28 +98,6 @@ int unregister_interrupt(int sig);

//#include <sys/param.h>

/* enable signal handler register access on different platforms
* check here for more:
* http://sourceforge.net/p/predef/wiki/OperatingSystems/
*/
#ifdef BSD // BSD = (FreeBSD, Darwin, ...)
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE
#include <ucontext.h>
#undef _XOPEN_SOURCE
#else
#include <ucontext.h>
#endif
#elif defined(__linux__)
#ifndef _GNU_SOURCE
#define GNU_SOURCE
#include <ucontext.h>
#undef GNU_SOURCE
#else
#include <ucontext.h>
#endif
#endif // BSD/Linux

#include "kernel_internal.h"
#include "sched.h"

Expand Down
2 changes: 1 addition & 1 deletion cpu/native/irq_cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ void native_isr_entry(int sig, siginfo_t *info, void *context)
#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;
#elif BSD
#elif defined(__FreeBSD__)
_native_saved_eip = ((struct sigcontext *)context)->sc_eip;
((struct sigcontext *)context)->sc_eip = (unsigned int)&_native_sig_leave_tramp;
#else
Expand Down
12 changes: 11 additions & 1 deletion cpu/native/net/tap.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
#undef _POSIX_C_SOURCE
#include <ifaddrs.h>
#include <net/if_dl.h>

#elif defined(__FreeBSD__)
#include <sys/socket.h>
#include <net/if.h>
#include <ifaddrs.h>
#include <net/if_dl.h>

#else
#include <net/if.h>
#include <linux/if_tun.h>
Expand Down Expand Up @@ -232,6 +239,9 @@ int tap_init(char *name)
#ifdef __MACH__ /* OSX */
char clonedev[255] = "/dev/"; /* XXX bad size */
strncpy(clonedev+5, name, 250);
#elif defined(__FreeBSD__)
char clonedev[255] = "/dev/"; /* XXX bad size */
strncpy(clonedev+5, name, 250);
#else /* Linux */
struct ifreq ifr;
const char *clonedev = "/dev/net/tun";
Expand All @@ -242,7 +252,7 @@ int tap_init(char *name)
err(EXIT_FAILURE, "open(%s)", clonedev);
}

#ifdef __MACH__ /* OSX */
#if (defined(__MACH__) || defined(__FreeBSD__)) /* OSX/FreeBSD */
struct ifaddrs* iflist;
if (getifaddrs(&iflist) == 0) {
for (struct ifaddrs *cur = iflist; cur; cur = cur->ifa_next) {
Expand Down
3 changes: 3 additions & 0 deletions cpu/native/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ ssize_t _native_write(int fd, const void *buf, size_t count)
return r;
}

#if defined(__FreeBSD__)
#undef putchar
#endif
int putchar(int c) {
_native_write(STDOUT_FILENO, &c, 1);
return 0;
Expand Down
54 changes: 54 additions & 0 deletions cpu/native/tapsetup-freebsd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/sh

COMMAND=${1}
COUNT=${2}

DEFCOUNT="2"

if [ -z "${USER}" ]; then
echo 'need to export $USER'
exit 1
fi
if [ -z "${COMMAND}" ]; then
echo "usage: $(basename $0) <create [count]|delete>"
exit 1
fi

if [ "${COMMAND}" = 'create' ]; then
if [ -z "${COUNT}" ]; then
COUNT="${DEFCOUNT}"
fi

# load kernel modules
sudo kldload if_tap
sudo kldload if_bridge

# set permissions
sudo sysctl net.link.tap.user_open=1

# create network
echo "creating ${BRNAME} ..."
sudo ifconfig bridge0 create
sudo ifconfig bridge0 up

for N in $(seq 0 "$((COUNT - 1))"); do
sudo ifconfig tap${N} create
sudo chown ${USER} /dev/tap${N}
sudo ifconfig tap${N} up
sudo ifconfig bridge0 addm tap${N}
done

elif [ "${COMMAND}" = 'delete' ]; then
# reset permissions (devices already destroyed)
sudo sysctl net.link.tap.user_open=0

# unload kernel modules
sudo kldunload if_tap
sudo kldunload if_bridge

else
echo 'unknown command'
exit 1
fi

exit 0

0 comments on commit 4c66f72

Please sign in to comment.