r/programming • u/[deleted] • Nov 14 '16
Tutorial - Write a System Call
https://brennan.io/2016/11/14/kernel-dev-ep3/8
u/_Skuzzzy Nov 14 '16
Unrelated to the tutorial, but do people actually like this grey font stuff people have been doing recently? I always have to edit the CSS to make the font black in order to read these.
6
u/SuperImaginativeName Nov 14 '16
I don't know but the horizontal scrollbars that aren't needed are rage inducing.
2
Nov 15 '16
I love having half contrast when using my work laptop, helps me not see it so I can move onto something else!
I'm a "#FFF on #000" kind of guy but I can understand slightly lightened black text on pages but people forget that not every screen people use is 100% sRGB 10000:1 static contrast at 450 nits and go with almost full gray too often.
1
u/ryobiguy Nov 15 '16
No, that is hard to read, it has insufficient contrast between background and text. Actually I have a hard time reading more than a few sentence before feeling dizzy.
4
2
u/BAOLONGtrann Nov 15 '16
Nice read. It seems like implementing a syscall is actually pretty straightforward. However what kind of syscall should I try to implement rather than the printk hello world one. I really want to try this but have no idea what to actually implement. Can anyone give me some pointer?
4
u/tayo42 Nov 15 '16
You do anything to manipulate something in kernel space. If it's just for learning you could something like take a pid and make it owned by root.
0
5
u/stopczyk Nov 15 '16 edited Nov 15 '16
I'm sorry, but this is a typically bad "let's do kernel stuff" post. It contains some misinformation and lacks crucial pieces. Unfortunately documenting a reasonable setup is quite time consuming, so I'll only give an outline.
Interestingly it does suggest using a vm, but apparently the main reason is that the kernel is going to be recompiled (as opposed to just a module being loaded).
First of all you should not just run a kernel with the default config. There are many debugging options which when enabled help catch bugs which would not manifest themselves in your testing. Classic includes lock ordering violations, missing locking in the first place and sleeping when sleeping is prohibited (e.g. while holding a spin lock).
The purpose of using a vm is not only to have a safe place to run the kernel in, but also to be able to gather debugging data or even attach with a debugger. For instance, qemu provides a gdb stub. Since an oopsing kernel can provide a lot of data, which scrolls past the screen, it only makes sense to enable serial console output with the kernel log redirected there and start logging it.
For a convenient compile + boot cycle, qemu allows you to pass both the kernel and initrd on command line. That is, you would not compile in the target vm, but on the host or another vm.
With this out of the way, let's look at the claims.
First somewhat a nitpick:
While true, this is a legacy interface. x86-64 has a dedicated "syscall" instruction and that's what's being used. Even the 32-bit variant has a dedicated instruction ("sysenter").
The syscall itself is not bad:
Why does this repeat the size as opposed to using sizeof(buf)?
Similarly, why not sizeof(buf) - 1?
The actual issue I want to comment on is this:
This is incorrect on most architectures, including x86. Normally there are no address space changes when you switch to the kernel and in fact, for toy purposes, you can change the syscall to just do printk(KERN_INFO "msg [%s]\n", msg);. Userspace-provided addresses are accessed with special primitives, because they can be bogus, point to the kernel, or subject to a page fault (maybe swapped out, or maybe you would write and copy on write will come into play) and perhaps in few more scenarios. The kernel must be able to deal with all that and that's what the primitives are for. In fact, newer processors start getting hardware protection from unintended accesses.
That said, playing with the kernel is great and nothing to be scared of, but it has to be done with care to not misinform yourself. Unfortunately almost everything one can find online about the subject is of questionable quality at best.