Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions kern/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@

#include <linux/version.h>

#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,1,0)
#if KERNEL_VERSION(3, 1, 0) <= LINUX_VERSION_CODE
#include <asm/fpu/api.h>
#else
#include <asm/i387.h>
#endif

#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,1,0)
#if KERNEL_VERSION(4, 1, 0) <= LINUX_VERSION_CODE
#include <asm/fpu/internal.h>
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
#elif KERNEL_VERSION(3, 4, 0) <= LINUX_VERSION_CODE
#include <asm/fpu-internal.h>
#endif

#if !defined(VMX_EPT_AD_BIT)
#define VMX_EPT_AD_BIT (1ull << 21)
#define VMX_EPT_AD_ENABLE_BIT (1ull << 6)
#define VMX_EPT_AD_BIT BIT_ULL(21)
#define VMX_EPT_AD_ENABLE_BIT BIT_ULL(6)
#endif

#ifndef VMX_EPT_EXTENT_INDIVIDUAL_BIT
#define VMX_EPT_EXTENT_INDIVIDUAL_BIT (1ull << 24)
#define VMX_EPT_EXTENT_INDIVIDUAL_BIT BIT_ULL(24)
#endif

#ifndef X86_CR4_PCIDE
Expand All @@ -40,17 +40,16 @@
#define AR_TYPE_BUSY_64_TSS VMX_AR_TYPE_BUSY_64_TSS
#endif

#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,3,0)
#if KERNEL_VERSION(4, 3, 0) <= LINUX_VERSION_CODE
static inline struct page *alloc_pages_exact_node(int nid, gfp_t gfp_mask,
unsigned int order){
unsigned int order) {
return alloc_pages_node(nid, gfp_mask, order);
}
#endif


#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,1,0) & defined(_DO_FORK)
#if KERNEL_VERSION(4, 1, 0) <= LINUX_VERSION_CODE & defined(_DO_FORK)
typedef long (*do_fork_hack) (unsigned long, unsigned long, unsigned long,
int __user *, int __user *, unsigned long);
int __user *, int __user *, unsigned long);
static do_fork_hack __dune_do_fork = (do_fork_hack) _DO_FORK;
static inline long
dune_do_fork(unsigned long clone_flags, unsigned long stack_start,
Expand All @@ -70,12 +69,11 @@ dune_do_fork(unsigned long clone_flags, unsigned long stack_start,

memcpy(me, &tmp, sizeof(struct pt_regs));
return ret;

}
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,5,0) & defined(DO_FORK)
#elif KERNEL_VERSION(3, 5, 0) <= LINUX_VERSION_CODE & defined(DO_FORK)
typedef long (*do_fork_hack) (unsigned long, unsigned long, unsigned long,
int __user *, int __user *);
static do_fork_hack __dune_do_fork = (do_fork_hack) DO_FORK;
int __user *, int __user *);
static do_fork_hack __dune_do_fork = (do_fork_hack)DO_FORK;
static inline long
dune_do_fork(unsigned long clone_flags, unsigned long stack_start,
struct pt_regs *regs, unsigned long stack_size,
Expand All @@ -94,31 +92,32 @@ dune_do_fork(unsigned long clone_flags, unsigned long stack_start,

memcpy(me, &tmp, sizeof(struct pt_regs));
return ret;

}
#elif defined(DO_FORK)
typedef long (*do_fork_hack) (unsigned long, unsigned long,
struct pt_regs *, unsigned long,
int __user *, int __user *);
static do_fork_hack dune_do_fork = (do_fork_hack) DO_FORK;
struct pt_regs *, unsigned long,
int __user *, int __user *);
static do_fork_hack dune_do_fork = (do_fork_hack)DO_FORK;
#endif

#if LINUX_VERSION_CODE < KERNEL_VERSION(3,19,0)
#if KERNEL_VERSION(3, 19, 0) > LINUX_VERSION_CODE
static inline unsigned long __read_cr4(void)
{
return read_cr4();
}

static inline void cr4_set_bits(unsigned long mask)
{
write_cr4(read_cr4() | mask);
}

static inline void cr4_clear_bits(unsigned long mask)
{
write_cr4(read_cr4() & ~mask);
}
#endif

#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,1,0)
#if KERNEL_VERSION(4, 1, 0) <= LINUX_VERSION_CODE
static inline void compat_fpu_restore(void)
{
if (!current->thread.fpu.fpregs_active)
Expand All @@ -132,7 +131,7 @@ static inline void compat_fpu_restore(void)
}
#endif

#if LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0)
#if KERNEL_VERSION(3, 18, 0) > LINUX_VERSION_CODE
#define _PAGE_CACHE_MODE_WB _PAGE_CACHE_WB
#define _PAGE_CACHE_MODE_WC _PAGE_CACHE_WC
static inline long pgprot2cachemode(pgprot_t pgprot)
Expand Down
31 changes: 18 additions & 13 deletions kern/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,22 @@ static int dune_is_in_guest(void)

static int dune_is_user_mode(void)
{
return 0;
return 0;
}

static unsigned long dune_get_guest_ip(void)
{
unsigned long long ip = 0;

if (__this_cpu_read(local_vcpu))
ip = vmcs_readl(GUEST_RIP);
return ip;
}

static struct perf_guest_info_callbacks dune_guest_cbs = {
.is_in_guest = dune_is_in_guest,
.is_user_mode = dune_is_user_mode,
.get_guest_ip = dune_get_guest_ip,
.is_in_guest = dune_is_in_guest,
.is_user_mode = dune_is_user_mode,
.get_guest_ip = dune_get_guest_ip,
};

static int dune_enter(struct dune_config *conf, int64_t *ret)
Expand All @@ -66,15 +67,15 @@ static int dune_enter(struct dune_config *conf, int64_t *ret)
}

static long dune_dev_ioctl(struct file *filp,
unsigned int ioctl, unsigned long arg)
unsigned int ioctl, unsigned long arg)
{
long r = -EINVAL;
struct dune_config conf;
struct dune_layout layout;

switch (ioctl) {
case DUNE_ENTER:
r = copy_from_user(&conf, (int __user *) arg,
r = copy_from_user(&conf, (int __user *)arg,
sizeof(struct dune_config));
if (r) {
r = -EIO;
Expand All @@ -95,13 +96,15 @@ static long dune_dev_ioctl(struct file *filp,

case DUNE_GET_SYSCALL:
rdmsrl(MSR_LSTAR, r);
printk(KERN_INFO "R %lx\n", (unsigned long) r);
pr_info("R %lx\n", (unsigned long)r);
break;

case DUNE_GET_LAYOUT:
layout.phys_limit = (1UL << boot_cpu_data.x86_phys_bits);
layout.base_map = LG_ALIGN(current->mm->mmap_base) - GPA_MAP_SIZE;
layout.base_stack = LG_ALIGN(current->mm->start_stack) - GPA_STACK_SIZE;
layout.base_map = LG_ALIGN(current->mm->mmap_base) -
GPA_MAP_SIZE;
layout.base_stack = LG_ALIGN(current->mm->start_stack) -
GPA_STACK_SIZE;
r = copy_to_user((void __user *)arg, &layout,
sizeof(struct dune_layout));
if (r) {
Expand Down Expand Up @@ -151,18 +154,20 @@ static struct miscdevice dune_dev = {
static int __init dune_init(void)
{
int r;

perf_register_guest_info_callbacks(&dune_guest_cbs);

printk(KERN_ERR "Dune module loaded\n");
pr_err("Dune module loaded\n");

if ((r = vmx_init())) {
printk(KERN_ERR "dune: failed to initialize vmx\n");
r = vmx_init();
if (r) {
pr_err("dune: failed to initialize vmx\n");
return r;
}

r = misc_register(&dune_dev);
if (r) {
printk(KERN_ERR "dune: misc device register failed\n");
pr_err("dune: misc device register failed\n");
vmx_exit();
}

Expand Down
16 changes: 9 additions & 7 deletions kern/dune.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ struct dune_config {
__u64 cr3;
__s64 status;
__u64 vcpu;
} __attribute__((packed));
} __packed;

struct dune_layout {
__u64 phys_limit;
__u64 base_map;
__u64 base_stack;
} __attribute__((packed));
} __packed;

struct dune_trap_regs {
__u64 rax;
Expand All @@ -74,9 +74,9 @@ struct dune_trap_regs {
__u64 r15;
__u64 rip;
__u64 rflags;
} __attribute__((packed));
} __packed;

typedef void (* dune_trap_notify_func)(struct dune_trap_regs *, void *);
typedef void (*dune_trap_notify_func)(struct dune_trap_regs*, void*);

struct dune_trap_config {
__u64 trigger_rip;
Expand All @@ -85,10 +85,12 @@ struct dune_trap_config {
__u64 regs_size;
void *priv;
__u8 delay;
} __attribute__((packed));
} __packed;

#define GPA_STACK_SIZE ((unsigned long) 1 << 30) /* 1 gigabyte */
#define GPA_MAP_SIZE (((unsigned long) 1 << 36) - GPA_STACK_SIZE) /* 63 gigabytes */
/* 1 gigabyte */
#define GPA_STACK_SIZE ((unsigned long)1 << 30)
/* 63 gigabytes */
#define GPA_MAP_SIZE (((unsigned long)1 << 36) - GPA_STACK_SIZE)
#define LG_ALIGN(addr) ((addr + (1 << 30) - 1) & ~((1 << 30) - 1))

#endif /* __ASSEMBLY__ */
Expand Down
Loading