diff --git a/README.adoc b/README.adoc index 8ca92a3..509d7dc 100644 --- a/README.adoc +++ b/README.adoc @@ -2608,7 +2608,7 @@ traps: ring0.out[55] general protection ip:40054c sp:7fffffffec20 error:0 in rin Sources: * link:kernel_modules/ring0.c[] -* link:include/ring0.h[] +* link:lkmc/ring0.h[] * link:userland/ring0.c[] In both cases, we attempt to run the exact same code which is shared on the `ring0.h` header file. @@ -6425,7 +6425,7 @@ Outcome: the test passes: Sources: * link:kernel_modules/ioctl.c[] -* link:include/ioctl.h[] +* link:lkmc/ioctl.h[] * link:userland/kernel_modules/ioctl.c[] * link:rootfs_overlay/lkmc/ioctl.sh[] @@ -6510,7 +6510,7 @@ Outcome: the test passes: Sources: * link:kernel_modules/anonymous_inode.c[] -* link:include/anonymous_inode.h[] +* link:lkmc/anonymous_inode.h[] * link:userland/kernel_modules/anonymous_inode.c[] * link:rootfs_overlay/lkmc/anonymous_inode.sh[] @@ -6538,7 +6538,7 @@ Outcome: the test passes: Sources: * link:kernel_modules/netlink.c[] -* link:include/netlink.h[] +* link:lkmc/netlink.h[] * link:userland/kernel_modules/netlink.c[] * link:rootfs_overlay/lkmc/netlink.sh[] @@ -13481,11 +13481,13 @@ git -C "$(./getvar buildroot_source_dir)" checkout - === Directory structure -==== include directory +==== lkmc directory -link:include/[] contains headers that are shared across both kernel modules and userland structures. +link:lkmc/[] contains sources and headers that are shared across kernel modules, userland and baremetal examples. -They contain data structs and magic constant for kernel to userland communication. +We chose this awkward name so that our includes will have an `lkmc/` prefix. + +Another option would have been to name it as `includes/lkmc`, but that would make paths longer, and we might want to store source code in that directory as well in the future. ==== buildroot_packages directory diff --git a/build-modules b/build-modules index 2e6dca1..c64b246 100755 --- a/build-modules +++ b/build-modules @@ -3,6 +3,7 @@ import distutils.dir_util import os import platform +import shlex import shutil import common @@ -80,14 +81,18 @@ Place the modules on a separate magic directory from non --host builds. cc = '{} {}'.format(ccache, gcc) else: cc = gcc - if self.env['verbose']: - verbose = ['V=1'] - else: - verbose = [] if self.env['host']: linux_dir = os.path.join('/lib', 'modules', platform.uname().release, 'build') else: linux_dir = self.env['linux_build_dir'] + ccflags = [ + '-I', self.env['root_dir'], LF, + ] + make_args_extra = [] + if self.env['verbose']: + make_args_extra.extend(['V=1', LF]) + if self.env['force_rebuild']: + make_args_extra.extend(['-B', LF]) self.sh.run_cmd( ( [ @@ -95,13 +100,14 @@ Place the modules on a separate magic directory from non --host builds. '-j', str(self.env['nproc']), LF, 'ARCH={}'.format(self.env['linux_arch']), LF, 'CC={}'.format(cc), LF, + 'CCFLAGS={}'.format(self.sh.cmd_to_string(ccflags)), LF, 'CROSS_COMPILE={}'.format(prefix), LF, 'LINUX_DIR={}'.format(linux_dir), LF, 'M={}'.format(build_subdir), LF, 'OBJECT_FILES={}'.format(' '.join(object_files)), LF, ] + - self.sh.shlex_split(self.env['make_args']) + - verbose + make_args_extra + + self.sh.shlex_split(self.env['make_args']) ), cwd=os.path.join(self.env['kernel_modules_build_subdir']), ) diff --git a/common.py b/common.py index d7a6069..049c7ae 100644 --- a/common.py +++ b/common.py @@ -57,7 +57,7 @@ consts['userland_subdir'] = 'userland' consts['userland_source_dir'] = os.path.join(consts['root_dir'], consts['userland_subdir']) consts['userland_source_arch_dir'] = os.path.join(consts['userland_source_dir'], 'arch') consts['userland_build_ext'] = '.out' -consts['include_subdir'] = 'include' +consts['include_subdir'] = 'lkmc' consts['include_source_dir'] = os.path.join(consts['root_dir'], consts['include_subdir']) consts['submodules_dir'] = os.path.join(consts['root_dir'], 'submodules') consts['buildroot_source_dir'] = os.path.join(consts['submodules_dir'], 'buildroot') diff --git a/include/README.adoc b/include/README.adoc deleted file mode 100644 index f3e97b5..0000000 --- a/include/README.adoc +++ /dev/null @@ -1 +0,0 @@ -https://github.com/cirosantilli/linux-kernel-module-cheat#include-directory diff --git a/kernel_modules/Makefile b/kernel_modules/Makefile index 5ac980d..7f5d60a 100644 --- a/kernel_modules/Makefile +++ b/kernel_modules/Makefile @@ -1,15 +1,17 @@ -ifeq ($(OBJECT_FILES),) -# Hardcoding LKMC_MODULE_SUBDIRS here because is not defined. -obj-m += $(addsuffix .o, $(notdir $(basename $(filter-out %.mod.c, $(wildcard $(BR2_EXTERNAL_LKMC_PATH)/kernel_modules/*.c))))) -else # Trying to do: # $(MAKE) -C '$(LINUX_DIR)' M='$(M)' hello.ko hello2.ko # to restrict which modules are built leads to failures # when doing parallel builds. The only solution I could find # was to let the host select obj-m itself. obj-m += $(OBJECT_FILES) -endif -ccflags-y := -DDEBUG -g -std=gnu99 -Werror -Wno-declaration-after-statement -Wframe-larger-than=1000000000 +ccflags-y := \ + -DDEBUG \ + -ggdb3 \ + -std=gnu99 \ + -Werror \ + -Wframe-larger-than=1000000000 \ + -Wno-declaration-after-statement \ + $(CCFLAGS) .PHONY: all diff --git a/kernel_modules/anonymous_inode.c b/kernel_modules/anonymous_inode.c index e3dbd21..d060763 100644 --- a/kernel_modules/anonymous_inode.c +++ b/kernel_modules/anonymous_inode.c @@ -9,7 +9,7 @@ #include /* printk */ #include /* copy_from_user */ -#include "../include/anonymous_inode.h" +#include static struct dentry *debugfs_file; static u32 myval = 1; diff --git a/kernel_modules/ioctl.c b/kernel_modules/ioctl.c index 4079a50..7207683 100644 --- a/kernel_modules/ioctl.c +++ b/kernel_modules/ioctl.c @@ -5,7 +5,7 @@ #include /* printk */ #include /* copy_from_user, copy_to_user */ -#include "../include/ioctl.h" +#include static struct dentry *debugfs_file; diff --git a/kernel_modules/netlink.c b/kernel_modules/netlink.c index 34b5bca..0f012b6 100644 --- a/kernel_modules/netlink.c +++ b/kernel_modules/netlink.c @@ -6,7 +6,7 @@ #include #include -#include "../include/netlink.h" +#include struct sock *nl_sk = NULL; diff --git a/kernel_modules/ring0.c b/kernel_modules/ring0.c index 6a5a268..211f342 100644 --- a/kernel_modules/ring0.c +++ b/kernel_modules/ring0.c @@ -3,7 +3,7 @@ #include #include -#include "../include/ring0.h" +#include static int myinit(void) { diff --git a/lkmc/README.adoc b/lkmc/README.adoc new file mode 100644 index 0000000..7c6fe7f --- /dev/null +++ b/lkmc/README.adoc @@ -0,0 +1 @@ +https://github.com/cirosantilli/linux-kernel-module-cheat#lkmc-directory diff --git a/include/anonymous_inode.h b/lkmc/anonymous_inode.h similarity index 51% rename from include/anonymous_inode.h rename to lkmc/anonymous_inode.h index d35fc06..93fed14 100644 --- a/include/anonymous_inode.h +++ b/lkmc/anonymous_inode.h @@ -1,5 +1,7 @@ -#ifndef IOCTL_H -#define IOCTL_H +/* https://github.com/cirosantilli/linux-kernel-module-cheat#anonymous-inode */ + +#ifndef LKMC_ANONYMOUS_INODE_H +#define LKMC_ANONYMOUS_INODE_H #include diff --git a/include/ioctl.h b/lkmc/ioctl.h similarity index 92% rename from include/ioctl.h rename to lkmc/ioctl.h index 14d6cdf..2469009 100644 --- a/include/ioctl.h +++ b/lkmc/ioctl.h @@ -1,5 +1,7 @@ -#ifndef IOCTL_H -#define IOCTL_H +/* https://github.com/cirosantilli/linux-kernel-module-cheat#ioctl */ + +#ifndef LKMC_IOCTL_H +#define LKMC_IOCTL_H #include diff --git a/include/netlink.h b/lkmc/netlink.h similarity index 72% rename from include/netlink.h rename to lkmc/netlink.h index 927f51d..c31abf7 100644 --- a/include/netlink.h +++ b/lkmc/netlink.h @@ -1,5 +1,7 @@ -#ifndef NETLINK_H -#define NETLINK_H +/* https://github.com/cirosantilli/linux-kernel-module-cheat#netlink-sockets */ + +#ifndef LKMC_NETLINK_H +#define LKMC_NETLINK_H /* Socket identifier, matches userland. TODO can be anything? * Is there a more scalable way to do it? E.g. ioctl device, diff --git a/include/ring0.h b/lkmc/ring0.h similarity index 93% rename from include/ring0.h rename to lkmc/ring0.h index 01472cb..b49257c 100644 --- a/include/ring0.h +++ b/lkmc/ring0.h @@ -1,3 +1,8 @@ +/* https://github.com/cirosantilli/linux-kernel-module-cheat#ring0 */ + +#ifndef LKMC_RING0_H +#define LKMC_RING0_H + #if defined(__x86_64__) || defined(__i386__) #ifdef THIS_MODULE diff --git a/userland/arch/x86_64/c/ring0.c b/userland/arch/x86_64/c/ring0.c index bbdbdb0..821d953 100644 --- a/userland/arch/x86_64/c/ring0.c +++ b/userland/arch/x86_64/c/ring0.c @@ -3,7 +3,7 @@ #include #include -#include +#include int main(void) { LkmcRing0Regs ring0_regs; diff --git a/userland/kernel_modules/anonymous_inode.c b/userland/kernel_modules/anonymous_inode.c index 3e0d246..3d16086 100644 --- a/userland/kernel_modules/anonymous_inode.c +++ b/userland/kernel_modules/anonymous_inode.c @@ -10,7 +10,7 @@ #include #include /* sleep */ -#include +#include int main(int argc, char **argv) { char buf[1024]; diff --git a/userland/kernel_modules/ioctl.c b/userland/kernel_modules/ioctl.c index 660e873..7c149ad 100644 --- a/userland/kernel_modules/ioctl.c +++ b/userland/kernel_modules/ioctl.c @@ -10,7 +10,7 @@ #include #include -#include +#include int main(int argc, char **argv) { char *ioctl_path; diff --git a/userland/kernel_modules/netlink.c b/userland/kernel_modules/netlink.c index 12912c0..f751ed4 100644 --- a/userland/kernel_modules/netlink.c +++ b/userland/kernel_modules/netlink.c @@ -7,7 +7,7 @@ #include #include -#include +#include #define MAX_PAYLOAD 1024