forked from rcore-os/RVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KO: add a simple linux kernel module
- Loading branch information
1 parent
e91d625
commit 5adba46
Showing
5 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
Language: Cpp | ||
BasedOnStyle: LLVM | ||
IndentWidth: 4 | ||
ColumnLimit: 120 | ||
AllowShortIfStatementsOnASingleLine: true | ||
AllowShortBlocksOnASingleLine: Empty | ||
AlignConsecutiveMacros: true | ||
PointerAlignment: Left | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.*.cmd | ||
*.a | ||
*.ko | ||
*.o | ||
*.mod.c | ||
*.mod | ||
Module.symvers | ||
modules.order |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export KDIR ?= /lib/modules/$(shell uname -r)/build | ||
|
||
module := rvm | ||
|
||
ccflags-y := -I$(src)/include | ||
obj-m += rvm.o | ||
|
||
all: build | ||
|
||
build: | ||
$(MAKE) -C $(KDIR) M=$(PWD) CC=$(CC) | ||
|
||
clean: | ||
$(MAKE) -C $(KDIR) M=$(PWD) CC=$(CC) clean | ||
|
||
insmod: rmmod | ||
sudo dmesg -C | ||
sudo insmod $(module).ko | ||
sudo chown root:kvm /dev/rvm | ||
sudo chmod 660 /dev/rvm | ||
dmesg | ||
|
||
rmmod: | ||
ifneq ($(shell lsmod | grep rvm),) | ||
sudo rmmod $(module) | ||
endif | ||
|
||
test: rmmod | ||
sudo dmesg -C | ||
sudo insmod $(module).ko | ||
sudo rmmod $(module).ko | ||
dmesg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#ifndef RVM_H | ||
#define RVM_H | ||
|
||
#include <linux/types.h> | ||
|
||
#define RVM_IO 0xAE00 | ||
#define RVM_GUEST_CREATE (RVM_IO + 0x01) | ||
#define RVM_GUEST_ADD_MEMORY_REGION (RVM_IO + 0x02) | ||
#define RVM_GUEST_SET_TRAP (RVM_IO + 0x03) | ||
#define RVM_VCPU_CREATE (RVM_IO + 0x11) | ||
#define RVM_VCPU_RESUME (RVM_IO + 0x12) | ||
#define RVM_VCPU_READ_STATE (RVM_IO + 0x13) | ||
#define RVM_VCPU_WRITE_STATE (RVM_IO + 0x14) | ||
#define RVM_VCPU_INTERRUPT (RVM_IO + 0x15) | ||
|
||
enum rvm_trap_kind { | ||
RVM_TRAP_KIND_BELL = 0, | ||
RVM_TRAP_KIND_MEM = 1, | ||
RVM_TRAP_KIND_IO = 2, | ||
}; | ||
|
||
enum rvm_exit_packet_kind { | ||
RVM_EXIT_PKT_KIND_GUEST_BELL = 1, | ||
RVM_EXIT_PKT_KIND_GUEST_IO = 2, | ||
RVM_EXIT_PKT_KIND_GUEST_MMIO = 3, | ||
RVM_EXIT_PKT_KIND_GUEST_VCPU = 4, | ||
}; | ||
|
||
enum rvm_vcpu_read_write_kind { | ||
RVM_VCPU_STATE = 0, | ||
RVM_VCPU_IO = 1, | ||
}; | ||
|
||
struct rvm_vcpu_state { | ||
uint64_t rax; | ||
uint64_t rcx; | ||
uint64_t rdx; | ||
uint64_t rbx; | ||
uint64_t rsp; | ||
uint64_t rbp; | ||
uint64_t rsi; | ||
uint64_t rdi; | ||
uint64_t r8; | ||
uint64_t r9; | ||
uint64_t r10; | ||
uint64_t r11; | ||
uint64_t r12; | ||
uint64_t r13; | ||
uint64_t r14; | ||
uint64_t r15; | ||
// Contains only the user-controllable lower 32-bits. | ||
uint64_t rflags; | ||
}; | ||
|
||
typedef struct rvm_io_value rvm_vcpu_io; | ||
|
||
struct rvm_io_value { | ||
uint8_t access_size; | ||
union { | ||
uint8_t u8; | ||
uint16_t u16; | ||
uint32_t u32; | ||
uint8_t buf[4]; | ||
}; | ||
}; | ||
|
||
struct rvm_exit_io_packet { | ||
uint16_t port; | ||
uint8_t access_size; | ||
bool is_input; | ||
bool is_string; | ||
bool is_repeat; | ||
union { | ||
uint8_t u8; | ||
uint16_t u16; | ||
uint32_t u32; | ||
uint8_t buf[4]; | ||
}; | ||
}; | ||
|
||
struct rvm_exit_mmio_packet { | ||
uint64_t addr; | ||
uint8_t inst_len; | ||
uint8_t inst_buf[15]; | ||
uint8_t default_operand_size; | ||
}; | ||
|
||
struct rvm_exit_packet { | ||
enum rvm_exit_packet_kind kind; | ||
uint64_t key; | ||
union { | ||
struct rvm_exit_io_packet io; | ||
struct rvm_exit_mmio_packet mmio; | ||
}; | ||
}; | ||
|
||
struct rvm_guest_add_memory_region_args { | ||
uint16_t vmid; | ||
uint64_t guest_start_paddr; | ||
uint64_t memory_size; | ||
}; | ||
struct rvm_guest_set_trap_args { | ||
uint16_t vmid; | ||
enum rvm_trap_kind kind; | ||
uint64_t addr; | ||
uint64_t size; | ||
uint64_t key; | ||
}; | ||
|
||
struct rvm_vcpu_create_args { | ||
uint16_t vmid; | ||
uint64_t entry; | ||
}; | ||
|
||
struct rvm_vcpu_resmue_args { | ||
uint16_t vcpu_id; | ||
struct rvm_exit_packet packet; | ||
}; | ||
|
||
struct rvm_vcpu_state_args { | ||
uint16_t vcpu_id; | ||
enum rvm_vcpu_read_write_kind kind; | ||
union { | ||
struct rvm_vcpu_state* vcpu_state_ptr; | ||
struct rvm_vcpu_io* vcpu_io_ptr; | ||
}; | ||
uint64_t buf_size; | ||
}; | ||
|
||
struct rvm_vcpu_interrupt_args { | ||
uint16_t vcpu_id; | ||
uint32_t vector; | ||
}; | ||
|
||
#endif // RVM_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <linux/fs.h> | ||
#include <linux/kernel.h> | ||
#include <linux/miscdevice.h> | ||
#include <linux/module.h> | ||
|
||
#include <rvm.h> | ||
|
||
static int rvm_open(struct inode* inode, struct file* file) { | ||
pr_info("[RVM] rvm_open\n"); | ||
return 0; | ||
} | ||
|
||
static int rvm_release(struct inode* inode, struct file* file) { | ||
pr_info("[RVM] rvm_release\n"); | ||
return 0; | ||
} | ||
|
||
static long rvm_ioctl(struct file* filp, unsigned int ioctl, unsigned long arg) { | ||
pr_info("[RVM] rvm_ioctl %x %lx\n", ioctl, arg); | ||
return -2; | ||
} | ||
|
||
static const struct file_operations rvm_fops = { | ||
.owner = THIS_MODULE, | ||
.open = rvm_open, | ||
.release = rvm_release, | ||
.unlocked_ioctl = rvm_ioctl, | ||
.llseek = no_llseek, | ||
}; | ||
|
||
struct miscdevice rvm_device = { | ||
.minor = 255, | ||
.name = "rvm", | ||
.fops = &rvm_fops, | ||
}; | ||
|
||
static int __init rvm_init(void) { | ||
int err = misc_register(&rvm_device); | ||
if (err) { | ||
pr_err("[RVM] cannot register misc device\n"); | ||
return err; | ||
} | ||
|
||
pr_info("[RVM] module_init\n"); | ||
return 0; | ||
} | ||
|
||
static void __exit rvm_exit(void) { | ||
misc_deregister(&rvm_device); | ||
pr_info("[RVM] module_exit\n"); | ||
} | ||
|
||
module_init(rvm_init); | ||
module_exit(rvm_exit); | ||
|
||
MODULE_LICENSE("GPL"); | ||
MODULE_AUTHOR("Yuekai Jia"); | ||
MODULE_DESCRIPTION("Rcore Virtual Machine."); | ||
MODULE_VERSION("0.1.0"); |