Kernel labs walkthrough
Step-by-step model, no kernel runs here
Follow each lab across the line between user space and the kernel: the system call I added to Linux 4.20, a module being loaded and removed, and a /proc file whose read handler returns the time.
Each step describes what the lab code does on Linux 4.20 (x86-64), with the terminal and dmesgoutput it produces. The timestamps are your computer's clock. Nothing is executed in a real kernel.
A new system call, number 335
Try: · (glibc reads it as an error) · (does not fit in an int)
Terminal
dmesg
The lab code
/* kernel/sys.c */
SYSCALL_DEFINE1(mycall, int, number)
{
printk("This is my first system call %d\n", number);
return number;
}
/* arch/x86/entry/syscalls/syscall_64.tbl */
335 64 mycall __x64_sys_mycall
/* arch/x86/include/asm/syscalls.h */
asmlinkage long sys_mycall(int);
/* testcall.c */
long ret = syscall(335, 666);Loading and removing a module
Terminal
dmesg
A clock in /proc
Terminal
dmesg
The read handler, and a version that ends the file
static ssize_t proc_read_clock(struct file *file, char __user *pszPage,
size_t size, loff_t *off)
{
struct timeval tv;
char buf[64];
int len;
do_gettimeofday(&tv);
len = sprintf(buf, "%ld %ld\n", tv.tv_sec, tv.tv_usec);
if (copy_to_user(pszPage, buf, len))
return -EFAULT;
return len; /* a fresh time on every read */
}
/* end-of-file version: respects size, advances *off, then returns 0 */
return simple_read_from_buffer(pszPage, size, off, buf, len);