From 5ee93e6452443a47ac5408e2dee51a666d1f1e32 Mon Sep 17 00:00:00 2001
From: Conrad Meyer <cem@FreeBSD.org>
Date: Mon, 9 Nov 2015 09:44:26 -0800
Subject: [PATCH 01/11] lkl: sys_statfs64 takes a middle size parameter

Signed-off-by: Conrad Meyer <cem@FreeBSD.org>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
---
 arch/lkl/include/uapi/asm/unistd.h | 3 ++-
 tools/lkl/lklfuse.c                | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch/lkl/include/uapi/asm/unistd.h b/arch/lkl/include/uapi/asm/unistd.h
index b2e89f198e32e1..224a4c08df046f 100644
--- a/arch/lkl/include/uapi/asm/unistd.h
+++ b/arch/lkl/include/uapi/asm/unistd.h
@@ -240,7 +240,8 @@ LKL_SYSCALL4(fstatat64, unsigned int, dfd, const char *, filname,
 	     struct lkl_stat64 *, statbuf, int, flag);
 LKL_SYSCALL2(stat64, const char *, filename, struct lkl_stat64 *, statbuf);
 LKL_SYSCALL2(lstat64, const char *, filename, struct lkl_stat64 *, statbuf);
-LKL_SYSCALL2(statfs64, const char *, path, struct lkl_statfs64 *, buf);
+LKL_SYSCALL3(statfs64, const char *, path, __lkl__kernel_size_t, sz,
+	     struct lkl_statfs64 *, buf);
 LKL_SYSCALL3(readlink, const char *, path, char *, buf, int, bufsiz);
 LKL_SYSCALL3(listxattr, const char *, path, char *, list, int, bufsiz);
 LKL_SYSCALL3(llistxattr, const char *, path, char *, list, int, bufsiz);
diff --git a/tools/lkl/lklfuse.c b/tools/lkl/lklfuse.c
index 4f0ae97c1cc1bb..8562b14523b882 100644
--- a/tools/lkl/lklfuse.c
+++ b/tools/lkl/lklfuse.c
@@ -259,7 +259,7 @@ static int lklfuse_statfs(const char *path, struct statvfs *stat)
 	long ret;
 	struct lkl_statfs64 lkl_statfs;
 
-	ret = lkl_sys_statfs64(path, &lkl_statfs);
+	ret = lkl_sys_statfs64(path, sizeof(lkl_statfs), &lkl_statfs);
 	if (ret < 0)
 		return ret;
 

From 69259d84bec842bc2ae151a0b4e5ab2307c4afa0 Mon Sep 17 00:00:00 2001
From: Conrad Meyer <cem@FreeBSD.org>
Date: Sun, 8 Nov 2015 20:03:47 -0800
Subject: [PATCH 02/11] lkl: headers_install: Use env(1) to locate python
 binary

Signed-off-by: Conrad Meyer <cem@FreeBSD.org>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
---
 arch/lkl/scripts/headers_install.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/lkl/scripts/headers_install.py b/arch/lkl/scripts/headers_install.py
index eb69a5e4098d86..cada8e9c66ba8b 100755
--- a/arch/lkl/scripts/headers_install.py
+++ b/arch/lkl/scripts/headers_install.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python
 import re, os, sys, argparse, multiprocessing
 
 header_paths = [ "include/uapi/", "arch/lkl/include/uapi/",

From 40ce53e46b0ca53f05d78d71907ad3c00ec66813 Mon Sep 17 00:00:00 2001
From: Conrad Meyer <cem@FreeBSD.org>
Date: Sun, 8 Nov 2015 20:09:08 -0800
Subject: [PATCH 03/11] lkl tools: Makefile: Drop lkl.o:lkl.o circular
 dependency

Signed-off-by: Conrad Meyer <cem@FreeBSD.org>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
---
 tools/lkl/Makefile | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tools/lkl/Makefile b/tools/lkl/Makefile
index 1a53e74d3beb9f..afa0265b4e3ca7 100644
--- a/tools/lkl/Makefile
+++ b/tools/lkl/Makefile
@@ -23,13 +23,13 @@ ifeq ($(shell $(LD) -r -print-output-format),elf64-x86-64)
 CFLAGS += -D_FILE_OFFSET_BITS=64
 endif
 
-lib_objs = $(patsubst %.c,%.o, $(lib_source)) lib/lkl.o
+lib_objs = $(patsubst %.c,%.o, $(lib_source))
 objs = $(patsubst %.c,%.o, $(source))
 execs += $(patsubst %.c,%, $(source))
 
 all: lib/liblkl.a $(execs)
 
-lib/liblkl.a: $(lib_objs)
+lib/liblkl.a: $(lib_objs) lib/lkl.o
 	$(AR) -rc $@ $^
 
 lib/lkl.o:
@@ -44,7 +44,7 @@ $(objs): lib/lkl.o
 $(execs): lib/liblkl.a
 
 clean:
-	-rm -rf include/lkl/ lib/liblkl.a $(lib_objs) $(objs) $(execs)
+	-rm -rf include/lkl/ lib/liblkl.a $(lib_objs) lib/lkl.o $(objs) $(execs)
 
 fs2tar: LDFLAGS += -larchive
 lklfuse: LDFLAGS += -lfuse

From 4efe4427a9556bcafdb7281006906ec64516ef98 Mon Sep 17 00:00:00 2001
From: Conrad Meyer <cem@FreeBSD.org>
Date: Sun, 8 Nov 2015 20:22:01 -0800
Subject: [PATCH 04/11] lkl: convert makefile echo \t to inline tab

Signed-off-by: Conrad Meyer <cem@FreeBSD.org>
[Octavian: -e does not work from make with dash switch to using inline tab]
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
---
 arch/lkl/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/lkl/Makefile b/arch/lkl/Makefile
index 51ad096f6ee99a..c3bbf01c6f18ad 100644
--- a/arch/lkl/Makefile
+++ b/arch/lkl/Makefile
@@ -22,7 +22,7 @@ lkl.o: vmlinux
 	$(OBJCOPY) $(foreach sym,$(LKL_ENTRY_POINTS),-G$(prefix)$(sym)) vmlinux lkl.o
 
 install: lkl.o __headers
-	@echo "  INSTALL\t$(INSTALL_PATH)/lib/lkl.o"
+	@echo "  INSTALL	$(INSTALL_PATH)/lib/lkl.o"
 	@cp lkl.o $(INSTALL_PATH)/lib/
 	@arch/lkl/scripts/headers_install.py \
 		$(subst -j,-j$(shell nproc),$(findstring -j,$(MAKEFLAGS))) \

From ed4362d16f198542afc8ad624faa09fad2e1e33a Mon Sep 17 00:00:00 2001
From: Conrad Meyer <cem@FreeBSD.org>
Date: Sun, 8 Nov 2015 20:42:56 -0800
Subject: [PATCH 05/11] lkl tools: Add kernel clean to clean target

Signed-off-by: Conrad Meyer <cem@FreeBSD.org>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
---
 tools/lkl/Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/lkl/Makefile b/tools/lkl/Makefile
index afa0265b4e3ca7..7e468a34f2f3be 100644
--- a/tools/lkl/Makefile
+++ b/tools/lkl/Makefile
@@ -45,6 +45,7 @@ $(execs): lib/liblkl.a
 
 clean:
 	-rm -rf include/lkl/ lib/liblkl.a $(lib_objs) lib/lkl.o $(objs) $(execs)
+	$(MAKE) -C ../.. ARCH=lkl clean
 
 fs2tar: LDFLAGS += -larchive
 lklfuse: LDFLAGS += -lfuse

From 2bd6dcada8232e252583a25c10bbe7557904a072 Mon Sep 17 00:00:00 2001
From: Conrad Meyer <cem@FreeBSD.org>
Date: Sun, 8 Nov 2015 22:21:50 -0800
Subject: [PATCH 06/11] tools lkl: Use OUTPUT_FORMAT variable instead of
 duplicating LD invocation

Signed-off-by: Conrad Meyer <cem@FreeBSD.org>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
---
 tools/lkl/Makefile | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/tools/lkl/Makefile b/tools/lkl/Makefile
index 7e468a34f2f3be..6ea6e3d87160ac 100644
--- a/tools/lkl/Makefile
+++ b/tools/lkl/Makefile
@@ -6,20 +6,24 @@ AR=$(CROSS_COMPILE)ar
 LD=$(CROSS_COMPILE)ld
 endif
 
+OUTPUT_FORMAT=$(shell $(LD) -r -print-output-format)
+
 lib_source = $(filter-out %-host.c,$(wildcard lib/*.c))
 source = $(wildcard tests/*.c)
-ifneq (,$(filter $(shell $(LD) -r -print-output-format),elf64-x86-64 elf32-i386))
+ifneq (,$(filter $(OUTPUT_FORMAT),elf64-x86-64 elf32-i386))
 source += $(wildcard *.c)
 lib_source += lib/posix-host.c
 LDFLAGS += -lpthread -lrt
 source += $(wildcard *.c)
 execs =  cpfromfs
-else ifeq ($(shell $(LD) -r -print-output-format),pe-i386)
+else ifeq ($(OUTPUT_FORMAT),pe-i386)
 lib_source += lib/nt-host.c
 KOPT="KALLSYMS_EXTRA_PASS=1"
+else
+$(error Unrecognized platform: $(OUTPUT_FORMAT))
 endif
 
-ifeq ($(shell $(LD) -r -print-output-format),elf64-x86-64)
+ifneq (,$(filter $(OUTPUT_FORMAT),elf64-x86-64))
 CFLAGS += -D_FILE_OFFSET_BITS=64
 endif
 

From 5b51c3171ba0b24d697880b63e33e67abe14357d Mon Sep 17 00:00:00 2001
From: Conrad Meyer <cem@FreeBSD.org>
Date: Sun, 8 Nov 2015 22:22:05 -0800
Subject: [PATCH 07/11] lkl tools: build with GCC, turn on the optimizer and
 more warnings

Signed-off-by: Conrad Meyer <cem@FreeBSD.org>
[Octavian: squashed commits, removed -Werror]
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
---
 tools/lkl/Makefile | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/tools/lkl/Makefile b/tools/lkl/Makefile
index 6ea6e3d87160ac..4c0227922be17a 100644
--- a/tools/lkl/Makefile
+++ b/tools/lkl/Makefile
@@ -1,9 +1,12 @@
-CFLAGS := -Iinclude -Wall -g
+CFLAGS := -Iinclude -Wall -g -O2 -Wextra -Wno-unused-parameter \
+	  -Wno-missing-field-initializers -fno-strict-aliasing
 
 ifdef CROSS_COMPILE
 CC=$(CROSS_COMPILE)gcc
 AR=$(CROSS_COMPILE)ar
 LD=$(CROSS_COMPILE)ld
+else
+CC=gcc
 endif
 
 OUTPUT_FORMAT=$(shell $(LD) -r -print-output-format)

From ce68a0cc7f7e646fe21d71f3541fe17f625b12a9 Mon Sep 17 00:00:00 2001
From: Conrad Meyer <cem@FreeBSD.org>
Date: Sun, 8 Nov 2015 22:24:54 -0800
Subject: [PATCH 08/11] lkl tools: Fix compiler warnings
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

cptofs.c: In function ‘main’:
cptofs.c:429:2: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
  if (disk_id < 0) {

fs2tar.c:355:2: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
  if (disk_id < 0) {
  ^
tests/boot.c: In function ‘printk’:
tests/boot.c:54:8: error: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Werror=unused-result]
   write(STDOUT_FILENO, str, len);
        ^
lib/posix-host.c: In function ‘print’:
lib/posix-host.c:20:7: error: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Werror=unused-result]
  write(STDOUT_FILENO, str, len);

lib/fs.c: In function ‘lkl_mount_dev’:
lib/fs.c:72:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (mnt_str_len < sizeof("/mnt/xxxxxxxx"))
                  ^
lib/utils.c: In function ‘lkl_strerror’:
lib/utils.c:147:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (err >= sizeof(lkl_err_strings) / sizeof(const char *))

lib/virtio.c: In function ‘virtio_read’:
lib/virtio.c:216:19: warning: ‘val’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  *(uint32_t *)res = htole32(val);

lklfuse.c: In function ‘lklfuse_readlink’:
lklfuse.c:136:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (ret == len)
          ^
lklfuse.c: In function ‘lklfuse_read’:
lklfuse.c:234:23: warning: signed and unsigned type in conditional expression [-Wsign-compare]
  return ret < 0 ? ret : orig_size - size;
                       ^
lklfuse.c: In function ‘lklfuse_write’:
lklfuse.c:253:23: warning: signed and unsigned type in conditional expression [-Wsign-compare]
  return ret < 0 ? ret : orig_size - size;

Signed-off-by: Conrad Meyer <cem@FreeBSD.org>
[Octavian: change lkl_mount_dev to take unsigned int size arg, squashed into single commit]
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
---
 tools/lkl/cptofs.c         |  5 +++--
 tools/lkl/fs2tar.c         |  9 +++++++--
 tools/lkl/include/lkl.h    |  2 +-
 tools/lkl/lib/fs.c         |  4 ++--
 tools/lkl/lib/posix-host.c |  4 +++-
 tools/lkl/lib/utils.c      |  2 +-
 tools/lkl/lib/virtio.c     |  5 ++---
 tools/lkl/lklfuse.c        | 12 ++++++------
 tools/lkl/tests/boot.c     |  4 +++-
 9 files changed, 28 insertions(+), 19 deletions(-)

diff --git a/tools/lkl/cptofs.c b/tools/lkl/cptofs.c
index 0017395eacf119..cd4c1e8c5950d3 100644
--- a/tools/lkl/cptofs.c
+++ b/tools/lkl/cptofs.c
@@ -425,11 +425,12 @@ int main(int argc, char **argv)
 		goto out;
 	}
 
-	disk_id = lkl_disk_add(bs);
-	if (disk_id < 0) {
+	ret = lkl_disk_add(bs);
+	if (ret < 0) {
 		fprintf(stderr, "can't add disk: %s\n", lkl_strerror(ret));
 		goto out_close;
 	}
+	disk_id = ret;
 
 	lkl_start_kernel(&lkl_host_ops, 100 * 1024 * 1024, "");
 
diff --git a/tools/lkl/fs2tar.c b/tools/lkl/fs2tar.c
index b74da6628fd85f..02f5ba75e0347b 100644
--- a/tools/lkl/fs2tar.c
+++ b/tools/lkl/fs2tar.c
@@ -1,3 +1,7 @@
+#ifdef __FreeBSD__
+#include <sys/param.h>
+#endif
+
 #include <stdio.h>
 #include <time.h>
 #include <argp.h>
@@ -351,11 +355,12 @@ int main(int argc, char **argv)
 		goto out;
 	}
 
-	disk_id = lkl_disk_add(bs);
-	if (disk_id < 0) {
+	ret = lkl_disk_add(bs);
+	if (ret < 0) {
 		fprintf(stderr, "can't add disk: %s\n", lkl_strerror(ret));
 		goto out_close;
 	}
+	disk_id = ret;
 
 	lkl_start_kernel(&lkl_host_ops, 10 * 1024 * 1024, "");
 
diff --git a/tools/lkl/include/lkl.h b/tools/lkl/include/lkl.h
index 220235b14bc3a1..a59c872fa4c613 100644
--- a/tools/lkl/include/lkl.h
+++ b/tools/lkl/include/lkl.h
@@ -57,7 +57,7 @@ int lkl_disk_add(union lkl_disk_backstore backstore);
  * @returns - 0 on success, a negative value on error
  */
 long lkl_mount_dev(unsigned int disk_id, const char *fs_type, int flags,
-		   void *data, char *mnt_str, int mnt_str_len);
+		   void *data, char *mnt_str, unsigned int mnt_str_len);
 
 /**
  * lkl_umount_dev - umount a disk
diff --git a/tools/lkl/lib/fs.c b/tools/lkl/lib/fs.c
index d09ae061a60abc..70e55f17da90ad 100644
--- a/tools/lkl/lib/fs.c
+++ b/tools/lkl/lib/fs.c
@@ -63,13 +63,13 @@ static long get_virtio_blkdev(int disk_id)
 }
 
 long lkl_mount_dev(unsigned int disk_id, const char *fs_type, int flags,
-		   void *data, char *mnt_str, int mnt_str_len)
+		   void *data, char *mnt_str, unsigned int mnt_str_len)
 {
 	char dev_str[] = { "/dev/xxxxxxxx" };
 	unsigned int dev;
 	int err;
 
-	if (mnt_str_len < sizeof("/mnt/xxxxxxxx"))
+	if (mnt_str_len < sizeof(dev_str))
 		return -LKL_ENOMEM;
 
 	dev = get_virtio_blkdev(disk_id);
diff --git a/tools/lkl/lib/posix-host.c b/tools/lkl/lib/posix-host.c
index 4bc1de70c3d715..5b30ee30f02393 100644
--- a/tools/lkl/lib/posix-host.c
+++ b/tools/lkl/lib/posix-host.c
@@ -17,7 +17,9 @@
 
 static void print(const char *str, int len)
 {
-	write(STDOUT_FILENO, str, len);
+	int ret __attribute__((unused));
+
+	ret = write(STDOUT_FILENO, str, len);
 }
 
 struct pthread_sem {
diff --git a/tools/lkl/lib/utils.c b/tools/lkl/lib/utils.c
index f8b676f983037a..d6a76e69556bc1 100644
--- a/tools/lkl/lib/utils.c
+++ b/tools/lkl/lib/utils.c
@@ -144,7 +144,7 @@ const char *lkl_strerror(int err)
 	if (err < 0)
 		err = -err;
 
-	if (err >= sizeof(lkl_err_strings) / sizeof(const char *))
+	if ((size_t)err >= sizeof(lkl_err_strings) / sizeof(const char *))
 		return "Bad error code";
 
 	return lkl_err_strings[err];
diff --git a/tools/lkl/lib/virtio.c b/tools/lkl/lib/virtio.c
index 17522b2aeb106a..5b961ef5e2a93b 100644
--- a/tools/lkl/lib/virtio.c
+++ b/tools/lkl/lib/virtio.c
@@ -165,7 +165,6 @@ static int virtio_read(void *data, int offset, void *res, int size)
 {
 	uint32_t val;
 	struct virtio_dev *dev = (struct virtio_dev *)data;
-	int ret = 0;
 
 	if (offset >= VIRTIO_MMIO_CONFIG) {
 		offset -= VIRTIO_MMIO_CONFIG;
@@ -210,12 +209,12 @@ static int virtio_read(void *data, int offset, void *res, int size)
 		val = dev->config_gen;
 		break;
 	default:
-		ret = -1;
+		return -1;
 	}
 
 	*(uint32_t *)res = htole32(val);
 
-	return ret;
+	return 0;
 }
 
 static inline void set_ptr_low(void **ptr, uint32_t val)
diff --git a/tools/lkl/lklfuse.c b/tools/lkl/lklfuse.c
index 8562b14523b882..0823ab07c268f8 100644
--- a/tools/lkl/lklfuse.c
+++ b/tools/lkl/lklfuse.c
@@ -133,8 +133,8 @@ static int lklfuse_readlink(const char *path, char *buf, size_t len)
 	if (ret < 0)
 		return ret;
 
-	if (ret == len)
-		ret  = len - 1;
+	if ((size_t)ret == len)
+		ret = len - 1;
 
 	buf[ret] = 0;
 
@@ -220,7 +220,7 @@ static int lklfuse_read(const char *path, char *buf, size_t size, off_t offset,
 		      struct fuse_file_info *fi)
 {
 	long ret;
-	int orig_size = size;
+	ssize_t orig_size = size;
 
 	do {
 		ret = lkl_sys_pread64(fi->fh, buf, size, offset);
@@ -231,7 +231,7 @@ static int lklfuse_read(const char *path, char *buf, size_t size, off_t offset,
 		buf += ret;
 	} while (size > 0);
 
-	return ret < 0 ? ret : orig_size - size;
+	return ret < 0 ? ret : orig_size - (ssize_t)size;
 
 }
 
@@ -239,7 +239,7 @@ static int lklfuse_write(const char *path, const char *buf, size_t size,
 		       off_t offset, struct fuse_file_info *fi)
 {
 	long ret;
-	int orig_size = size;
+	ssize_t orig_size = size;
 
 	do {
 		ret = lkl_sys_pwrite64(fi->fh, buf, size, offset);
@@ -250,7 +250,7 @@ static int lklfuse_write(const char *path, const char *buf, size_t size,
 		buf += ret;
 	} while (size > 0);
 
-	return ret < 0 ? ret : orig_size - size;
+	return ret < 0 ? ret : orig_size - (ssize_t)size;
 }
 
 
diff --git a/tools/lkl/tests/boot.c b/tools/lkl/tests/boot.c
index 8b401b729ba817..19d97c903b5987 100644
--- a/tools/lkl/tests/boot.c
+++ b/tools/lkl/tests/boot.c
@@ -50,8 +50,10 @@ static int parse_opt(int key, char *arg)
 
 void printk(const char *str, int len)
 {
+	int ret __attribute__((unused));
+
 	if (cla.printk)
-		write(STDOUT_FILENO, str, len);
+		ret = write(STDOUT_FILENO, str, len);
 }
 
 #define TEST(name) do_test(#name, test_##name)

From 3aa876882468d48ab90d4de9f95953a76dea1f0e Mon Sep 17 00:00:00 2001
From: Conrad Meyer <cem@FreeBSD.org>
Date: Sun, 8 Nov 2015 19:54:59 -0800
Subject: [PATCH 09/11] lkl: add support for 64bit FreeBSD

Signed-off-by: Conrad Meyer <cem@FreeBSD.org>
[Octavian: use OUTPUT_FORMAT to detect 64bit FreeBSD, squash commits]
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
---
 arch/lkl/Kconfig  | 1 +
 arch/lkl/Makefile | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/arch/lkl/Kconfig b/arch/lkl/Kconfig
index 567533bd0004c2..77fe2e3c2b7f2a 100644
--- a/arch/lkl/Kconfig
+++ b/arch/lkl/Kconfig
@@ -19,6 +19,7 @@ config LKL
        select PHYS_ADDR_T_64BIT if 64BIT
        select 64BIT if OUTPUT_FORMAT = "elf64-x86-64"
        select HAVE_UNDERSCORE_SYMBOL_PREFIX if OUTPUT_FORMAT = "pe-i386"
+       select 64BIT if OUTPUT_FORMAT = "elf64-x86-64-freebsd"
 
 config OUTPUTFORMAT
        string
diff --git a/arch/lkl/Makefile b/arch/lkl/Makefile
index c3bbf01c6f18ad..5fdec6426c19db 100644
--- a/arch/lkl/Makefile
+++ b/arch/lkl/Makefile
@@ -2,12 +2,14 @@ include arch/lkl/auto.conf
 
 KBUILD_CFLAGS += -fno-builtin
 
-ifeq ($(OUTPUT_FORMAT),elf64-x86-64)
+ifneq (,$(filter $(OUTPUT_FORMAT),elf64-x86-64 elf64-x86-64-freebsd))
 KBUILD_CFLAGS += -fPIC
 else ifeq ($(OUTPUT_FORMAT),pe-i386)
 prefix=_
 # workaround for #include_next<stdarg.h> errors
 LINUXINCLUDE := -isystem arch/lkl/include/system $(LINUXINCLUDE)
+else
+$(error Unrecognized platform: $(OUTPUT_FORMAT))
 endif
 
 LDFLAGS_vmlinux += -r

From dc4223e24a31873b3cf9cd64c00c2d6ea6ecea28 Mon Sep 17 00:00:00 2001
From: Conrad Meyer <cem@FreeBSD.org>
Date: Sun, 8 Nov 2015 20:03:17 -0800
Subject: [PATCH 10/11] lkl: On FreeBSD, use hw.ncpu sysctl instead of Linux
 nproc(1)

Signed-off-by: Conrad Meyer <cem@FreeBSD.org>
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
---
 arch/lkl/Makefile | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/arch/lkl/Makefile b/arch/lkl/Makefile
index 5fdec6426c19db..9b23c736fd9c35 100644
--- a/arch/lkl/Makefile
+++ b/arch/lkl/Makefile
@@ -12,6 +12,12 @@ else
 $(error Unrecognized platform: $(OUTPUT_FORMAT))
 endif
 
+ifneq (,$(filter $(OUTPUT_FORMAT),elf64-x86-64-freebsd))
+NPROC=$(shell sysctl -n hw.ncpu)
+else
+NPROC=$(shell nproc)
+endif
+
 LDFLAGS_vmlinux += -r
 LKL_ENTRY_POINTS := lkl_start_kernel lkl_sys_halt lkl_syscall lkl_trigger_irq \
 	lkl_get_free_irq lkl_put_irq
@@ -27,7 +33,7 @@ install: lkl.o __headers
 	@echo "  INSTALL	$(INSTALL_PATH)/lib/lkl.o"
 	@cp lkl.o $(INSTALL_PATH)/lib/
 	@arch/lkl/scripts/headers_install.py \
-		$(subst -j,-j$(shell nproc),$(findstring -j,$(MAKEFLAGS))) \
+		$(subst -j,-j$(NPROC),$(findstring -j,$(MAKEFLAGS))) \
 		$(INSTALL_PATH)/include
 
 archclean:

From 12f7c7375d8c212590edfb42acdf821f3e507169 Mon Sep 17 00:00:00 2001
From: Conrad Meyer <cem@FreeBSD.org>
Date: Wed, 11 Nov 2015 15:16:15 +0200
Subject: [PATCH 11/11] lkl tools: add support for 64bit FreeBSD

Signed-off-by: Conrad Meyer <cem@FreeBSD.org>
[Octavian: squash commits]
Signed-off-by: Octavian Purdila <octavian.purdila@intel.com>
---
 Documentation/lkl.txt      | 15 +++++++++++++++
 tools/lkl/Makefile         |  8 ++++++--
 tools/lkl/cptofs.c         |  4 ++++
 tools/lkl/lib/posix-host.c |  6 +++++-
 tools/lkl/lib/virtio.h     |  8 ++++++--
 5 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/Documentation/lkl.txt b/Documentation/lkl.txt
index c9e30f7455c86e..4da91f11d7f995 100644
--- a/Documentation/lkl.txt
+++ b/Documentation/lkl.txt
@@ -19,6 +19,21 @@ LKL is implemented as an architecture port in arch/lkl. It uses host operations
 defined by the application or a host library (tools/lkl/lib).
 
 
+Building LKL on FreeBSD
+-----------------------
+
+$ pkg install binutils gcc49
+
+If you don't have a gcc binary:
+$ ln -sf /usr/local/bin/gcc49 /usr/local/bin/gcc
+
+Prefer ports binutils:
+$ export PATH=/sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/lib64/ccache
+
+$ cd tools/lkl
+$ gmake
+
+
 Building LKL the host library and LKL applications
 --------------------------------------------------
 
diff --git a/tools/lkl/Makefile b/tools/lkl/Makefile
index 4c0227922be17a..6b50988d701e91 100644
--- a/tools/lkl/Makefile
+++ b/tools/lkl/Makefile
@@ -13,7 +13,7 @@ OUTPUT_FORMAT=$(shell $(LD) -r -print-output-format)
 
 lib_source = $(filter-out %-host.c,$(wildcard lib/*.c))
 source = $(wildcard tests/*.c)
-ifneq (,$(filter $(OUTPUT_FORMAT),elf64-x86-64 elf32-i386))
+ifneq (,$(filter $(OUTPUT_FORMAT),elf64-x86-64 elf32-i386 elf64-x86-64-freebsd))
 source += $(wildcard *.c)
 lib_source += lib/posix-host.c
 LDFLAGS += -lpthread -lrt
@@ -26,7 +26,7 @@ else
 $(error Unrecognized platform: $(OUTPUT_FORMAT))
 endif
 
-ifneq (,$(filter $(OUTPUT_FORMAT),elf64-x86-64))
+ifneq (,$(filter $(OUTPUT_FORMAT),elf64-x86-64 elf64-x86-64-freebsd))
 CFLAGS += -D_FILE_OFFSET_BITS=64
 endif
 
@@ -56,5 +56,9 @@ clean:
 
 fs2tar: LDFLAGS += -larchive
 lklfuse: LDFLAGS += -lfuse
+ifneq (,$(filter $(OUTPUT_FORMAT),elf64-x86-64-freebsd))
+cptofs: LDFLAGS += -largp
+fs2tar: LDFLAGS += -largp
+endif
 cpfromfs: cptofs
 	if ! [ -e $@ ]; then ln -s $< $@; fi
diff --git a/tools/lkl/cptofs.c b/tools/lkl/cptofs.c
index cd4c1e8c5950d3..051e1e961076ed 100644
--- a/tools/lkl/cptofs.c
+++ b/tools/lkl/cptofs.c
@@ -1,3 +1,7 @@
+#ifdef __FreeBSD__
+#include <sys/param.h>
+#endif
+
 #include <stdio.h>
 #include <time.h>
 #include <argp.h>
diff --git a/tools/lkl/lib/posix-host.c b/tools/lkl/lib/posix-host.c
index 5b30ee30f02393..71fa943bf206c5 100644
--- a/tools/lkl/lib/posix-host.c
+++ b/tools/lkl/lib/posix-host.c
@@ -1,5 +1,5 @@
 #include <pthread.h>
-#include <malloc.h>
+#include <stdlib.h>
 #include <sys/time.h>
 #include <time.h>
 #include <signal.h>
@@ -189,7 +189,11 @@ void fd_do_rw(union lkl_disk_backstore bs, unsigned int type, unsigned int prio,
 		break;
 	case LKL_DEV_BLK_TYPE_FLUSH:
 	case LKL_DEV_BLK_TYPE_FLUSH_OUT:
+#ifdef __linux__
 		err = fdatasync(bs.fd);
+#else
+		err = fsync(bs.fd);
+#endif
 		break;
 	default:
 		lkl_dev_blk_complete(bufs, LKL_DEV_BLK_STATUS_UNSUP, 0);
diff --git a/tools/lkl/lib/virtio.h b/tools/lkl/lib/virtio.h
index b76b18b74ca560..4ca774ba26e2e1 100644
--- a/tools/lkl/lib/virtio.h
+++ b/tools/lkl/lib/virtio.h
@@ -82,13 +82,17 @@ void virtio_dev_complete(struct virtio_dev_req *req, uint32_t len);
 	(type *)((char *)(ptr) - __builtin_offsetof(type, member))
 
 #ifndef __MINGW32__
-#include <endian.h>
+#ifdef __FreeBSD__
+#include <sys/endian.h>
 #else
+#include <endian.h>
+#endif  /* __FreeBSD__ */
+#else  /* !__MINGW32__ */
 #define le32toh(x) (x)
 #define le16toh(x) (x)
 #define htole32(x) (x)
 #define htole16(x) (x)
 #define le64toh(x) (x)
-#endif
+#endif  /* __MINGW32__ */
 
 #endif /* _LKL_LIB_VIRTIO_H */
