Skip to content

Commit f2bf644

Browse files
committed
Review and link kernel_module/user examples to README
1 parent 258add9 commit f2bf644

File tree

15 files changed

+121
-74
lines changed

15 files changed

+121
-74
lines changed

README.adoc

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4863,9 +4863,6 @@ sendkey shift-pgdown
48634863

48644864
===== Ctrl Alt Del
48654865

4866-
* https://superuser.com/questions/193652/does-linux-have-a-ctrlaltdel-equivalent/1324415#1324415
4867-
* https://unix.stackexchange.com/questions/42573/meaning-and-commands-for-ctrlaltdel/444969#444969
4868-
48694866
Reboot guest:
48704867

48714868
....
@@ -4952,9 +4949,12 @@ ever reaches the kernel tty layer, and it's up to the program
49524949
to decide what to do with it.
49534950
....
49544951

4955-
===== SysRq
4952+
Bibliography:
49564953

4957-
https://en.wikipedia.org/wiki/Magic_SysRq_key
4954+
* https://superuser.com/questions/193652/does-linux-have-a-ctrlaltdel-equivalent/1324415#1324415
4955+
* https://unix.stackexchange.com/questions/42573/meaning-and-commands-for-ctrlaltdel/444969#444969
4956+
4957+
===== SysRq
49584958

49594959
We cannot test these actual shortcuts on QEMU since the host captures them at a lower level, but from:
49604960

@@ -5004,6 +5004,8 @@ The bitmask is documented at:
50045004
less linux/Documentation/admin-guide/sysrq.rst
50055005
....
50065006

5007+
Bibliography: https://en.wikipedia.org/wiki/Magic_SysRq_key
5008+
50075009
==== TTY
50085010

50095011
In order to play with TTYs, do this:
@@ -5076,6 +5078,8 @@ Identify the current TTY with the command:
50765078
tty
50775079
....
50785080

5081+
Bibliography:
5082+
50795083
* https://unix.stackexchange.com/questions/270272/how-to-get-the-tty-in-which-bash-is-running/270372
50805084
* https://unix.stackexchange.com/questions/187319/how-to-get-the-real-name-of-the-controlling-terminal
50815085
* https://unix.stackexchange.com/questions/77796/how-to-get-the-current-terminal-name
@@ -5133,7 +5137,7 @@ insmod /kthread.ko
51335137

51345138
and switch between virtual terminals, to understand that the dmesg goes to whatever current virtual terminal you are on, but not the others, and not to the serial terminals.
51355139

5136-
See also:
5140+
Bibliography:
51375141

51385142
* https://serverfault.com/questions/119736/how-to-enable-multiple-virtual-consoles-on-linux
51395143
* https://github.com/mirror/busybox/blob/1_28_3/examples/inittab#L60
@@ -5197,7 +5201,7 @@ Instead, the shell appears on `/dev/tty7`.
51975201

51985202
If you run in <<graphic-mode>>, then you get a Penguin image for <<number-of-cores,every core>> above the console! https://askubuntu.com/questions/80938/is-it-possible-to-get-the-tux-logo-on-the-text-based-boot
51995203

5200-
This is due to the `CONFIG_LOGO=y` option which we enable by default.
5204+
This is due to the link:https://github.com/torvalds/linux/blob/v4.17/drivers/video/logo/Kconfig#L5[`CONFIG_LOGO=y`] option which we enable by default.
52015205

52025206
`reset` on the terminal then kills the poor penguins.
52035207

@@ -5350,7 +5354,7 @@ When I build it on Ubuntu 18.04 host, it does not generate any executable, so I'
53505354

53515355
=== Linux kernel testing
53525356

5353-
https://stackoverflow.com/questions/3177338/how-is-the-linux-kernel-tested
5357+
Bibliography: https://stackoverflow.com/questions/3177338/how-is-the-linux-kernel-tested
53545358

53555359
==== LTP
53565360

@@ -5417,8 +5421,6 @@ Some QEMU specific features to play with and limitations to cry over.
54175421

54185422
=== Snapshot
54195423

5420-
https://stackoverflow.com/questions/40227651/does-qemu-emulator-have-checkpoint-function/48724371#48724371
5421-
54225424
QEMU allows us to take snapshots at any time through the monitor.
54235425

54245426
You can then restore CPU, memory and disk state back at any time.
@@ -5493,6 +5495,8 @@ And the output is `0`.
54935495

54945496
Our setup does not allow for snapshotting while using <<initrd>>.
54955497

5498+
Bibliography: https://stackoverflow.com/questions/40227651/does-qemu-emulator-have-checkpoint-function/48724371#48724371
5499+
54965500
==== Snapshot internals
54975501

54985502
Snapshots are stored inside the `.qcow2` images themselves.
@@ -5918,8 +5922,6 @@ Rationale: we found out that the kernels that build for `qemu -M versatilepb` do
59185922

59195923
At the same time, we also found out that Versatile Express (`vexpress`) does support armv7, so maybe we could port it over, but I had lost interest at that point, and decided to just go with the simpler `-M virt` machine instead.
59205924

5921-
https://stackoverflow.com/questions/28315265/how-to-add-a-new-device-in-qemu-source-code/44612957#44612957
5922-
59235925
Uses:
59245926

59255927
* `hw/misc/lkmc_platform_device.c` minimal device added in our QEMU fork to `-M versatilepb`
@@ -5951,6 +5953,8 @@ The IRQ number `34` was found by on the dmesg after:
59515953
insmod /platform_device.ko
59525954
....
59535955

5956+
Bibliography: https://stackoverflow.com/questions/28315265/how-to-add-a-new-device-in-qemu-source-code/44612957#44612957
5957+
59545958
==== gem5 educational hardware models
59555959

59565960
TODO get some working!
@@ -5963,8 +5967,6 @@ This protocol allows sharing a mountable filesystem between guest and host.
59635967

59645968
With networking, it's boring, we can just use any of the old tools like sshfs and NFS.
59655969

5966-
https://superuser.com/questions/628169/how-to-share-a-directory-with-the-host-without-networking-in-qemu
5967-
59685970
One advantage of this method over NFS is that can run without `sudo` on host, or having to pass host credentials on guest for sshfs.
59695971

59705972
TODO performance compared to NFS.
@@ -6011,7 +6013,10 @@ security_model=mapped
60116013
+
60126014
writes from guest failed due to user mismatch problems: https://serverfault.com/questions/342801/read-write-access-for-passthrough-9p-filesystems-with-libvirt-qemu
60136015

6014-
The feature is documented at: https://wiki.qemu.org/Documentation/9psetup
6016+
Bibliography:
6017+
6018+
* https://superuser.com/questions/628169/how-to-share-a-directory-with-the-host-without-networking-in-qemu
6019+
* https://wiki.qemu.org/Documentation/9psetup
60156020

60166021
==== 9P gem5
60176022

@@ -6093,8 +6098,6 @@ We us this exact procedure to connect to <<gdbserver>>.
60936098

60946099
===== ssh into guest
60956100

6096-
https://unix.stackexchange.com/questions/124681/how-to-ssh-from-host-to-guest-using-qemu/307557#307557
6097-
60986101
Not enabled by default due to the build / runtime overhead. To enable, build with:
60996102

61006103
....
@@ -6115,6 +6118,8 @@ And finally on host:
61156118
ssh root@localhost -p 45456
61166119
....
61176120

6121+
Bibliography: https://unix.stackexchange.com/questions/124681/how-to-ssh-from-host-to-guest-using-qemu/307557#307557
6122+
61186123
===== gem5 host to guest networking
61196124

61206125
Could not do port forwarding from host to guest, and therefore could not use `gdbserver`: https://stackoverflow.com/questions/48941494/how-to-do-port-forwarding-from-guest-to-host-in-gem5
@@ -6133,9 +6138,7 @@ gives:
61336138
Could not open guest forwarding device 'guestfwd.tcp.45456'
61346139
....
61356140

6136-
Related:
6137-
6138-
* https://serverfault.com/questions/769874/how-to-forward-a-port-from-guest-to-host-in-qemu-kvm
6141+
Bibliography: https://serverfault.com/questions/769874/how-to-forward-a-port-from-guest-to-host-in-qemu-kvm
61396142

61406143
==== Secondary disk
61416144

@@ -6967,24 +6970,57 @@ Buildroot supports it, which makes everything just trivial:
69676970

69686971
....
69696972
./build -B 'BR2_PACKAGE_OPENBLAS=y' -k
6970-
./run -F '/openblas.out'
6973+
./run -F '/openblas.out; echo $?'
6974+
....
6975+
6976+
Outcome: the test passes:
6977+
6978+
....
6979+
0
69716980
....
69726981

69736982
Source: link:kernel_module/user/openblas.c[]
69746983

6984+
The test performs a general matrix multiplication:
6985+
6986+
....
6987+
| 1.0 -3.0 | | 1.0 2.0 1.0 | | 0.5 0.5 0.5 | | 11.0 - 9.0 5.0 |
6988+
1 * | 2.0 4.0 | * | -3.0 4.0 -1.0 | + 2 * | 0.5 0.5 0.5 | = | - 9.0 21.0 -1.0 |
6989+
| 1.0 -1.0 | | 0.5 0.5 0.5 | | 5.0 - 1.0 3.0 |
6990+
....
6991+
6992+
This can be deduced from the Fortran interfaces at: `out/x86_64/buildroot/build/openblas-*/reference/dgemmf.f`, which we can map to our call as:
6993+
6994+
....
6995+
C := alpha*op( A )*op( B ) + beta*C,
6996+
SUBROUTINE DGEMMF( TRANA, TRANB, M,N,K, ALPHA,A,LDA,B,LDB,BETA,C,LDC)
6997+
cblas_dgemm( CblasColMajor, CblasNoTrans, CblasTrans,3,3,2 ,1, A,3, B,3, 2 ,C,3 );
6998+
....
6999+
69757000
===== Eigen
69767001

6977-
Header only linear algebra library supported by Buildroot:
7002+
Header only linear algebra library with a mainline Buildroot package:
69787003

69797004
....
69807005
./build -B 'BR2_PACKAGE_EIGEN=y' -k
6981-
./run -F '/eigen_hello.out;/eigen_svd.out'
69827006
....
69837007

6984-
Source:
7008+
Just create an array and print it:
7009+
7010+
....
7011+
./run -F '/eigen.out'
7012+
....
7013+
7014+
Output:
69857015

6986-
* link:kernel_module/user/eigen.cpp[]
6987-
* link:kernel_module/user/eigen_svd.cpp[]
7016+
....
7017+
3 -1
7018+
2.5 1.5
7019+
....
7020+
7021+
Source: link:kernel_module/user/eigen.cpp[]
7022+
7023+
This example just creates a matrix and prints it out.
69887024

69897025
Tested on: link:http://github.com/cirosantilli/linux-kernel-module-cheat/commit/a4bdcf102c068762bb1ef26c591fcf71e5907525[a4bdcf102c068762bb1ef26c591fcf71e5907525]
69907026

@@ -7658,7 +7694,7 @@ which gives pretty close results, and serve as a nice sanity check that the cycl
76587694

76597695
It is also nice to see that `rdtsc` is a bit smaller than the `stats.txt` value, since the latter also includes the exec syscall for `m5`.
76607696

7661-
See also:
7697+
Bibliography:
76627698

76637699
* https://en.wikipedia.org/wiki/Time_Stamp_Counter
76647700
* https://stackoverflow.com/questions/9887839/clock-cycle-count-wth-gcc/9887979

kernel_module/user/anonymous_inode.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#anonymous-inode */
2+
13
#define _GNU_SOURCE
24
#include <errno.h>
35
#include <fcntl.h>

kernel_module/user/common.h

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33

44
#define _XOPEN_SOURCE 700
55
#include <fcntl.h> /* open */
6+
#include <math.h> /* fabs */
67
#include <stdint.h> /* uint64_t */
78
#include <stdlib.h> /* size_t */
9+
#include <stdio.h> /* snprintf */
10+
#include <sys/types.h>
811
#include <unistd.h> /* pread, sysconf */
12+
#include <stdbool.h>
913

1014
/* Format documented at:
1115
* https://github.com/torvalds/linux/blob/v4.9/Documentation/vm/pagemap.txt
12-
**/
16+
*/
1317
typedef struct {
1418
uint64_t pfn : 54;
1519
unsigned int soft_dirty : 1;
@@ -33,8 +37,12 @@ int pagemap_get_entry(PagemapEntry *entry, int pagemap_fd, uintptr_t vaddr)
3337

3438
nread = 0;
3539
while (nread < sizeof(data)) {
36-
ret = pread(pagemap_fd, &data, sizeof(data),
37-
(vaddr / sysconf(_SC_PAGE_SIZE)) * sizeof(data) + nread);
40+
ret = pread(
41+
pagemap_fd,
42+
&data,
43+
sizeof(data),
44+
(vaddr / sysconf(_SC_PAGE_SIZE)) * sizeof(data) + nread
45+
);
3846
nread += ret;
3947
if (ret <= 0) {
4048
return 1;
@@ -74,4 +82,18 @@ int virt_to_phys_user(uintptr_t *paddr, pid_t pid, uintptr_t vaddr)
7482
return 0;
7583
}
7684

85+
bool common_vector_equal(size_t n, double * v1, double * v2, double max_err)
86+
{
87+
double sum = 0.0;
88+
double diff;
89+
size_t i;
90+
for (i = 0; i < n; ++i) {
91+
diff = v1[i] - v2[i];
92+
sum += diff * diff;
93+
}
94+
if (sqrt(sum)/n > max_err)
95+
return false;
96+
return true;
97+
}
98+
7799
#endif

kernel_module/user/ctrl_alt_del.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#ctrl-alt-del */
2+
13
#define _XOPEN_SOURCE 700
24
#include <signal.h>
35
#include <stdio.h>

kernel_module/user/eigen_hello.cpp renamed to kernel_module/user/eigen.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
/* Adapted from: https://eigen.tuxfamily.org/dox/GettingStarted.html */
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#eigen
2+
* Adapted from: https://eigen.tuxfamily.org/dox/GettingStarted.html
3+
*/
24
#include <iostream>
35
#include <Eigen/Dense>
46
int main() {

kernel_module/user/eigen_svd.cpp

Lines changed: 0 additions & 24 deletions
This file was deleted.

kernel_module/user/libdrm_modeset.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Adapted from: https://github.com/dvdhrm/docs/blob/fad7c3203b14e67053e0fc41d8490138b8ff47dd/drm-howto/modeset.c */
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#drm
2+
* Adapted from: https://github.com/dvdhrm/docs/blob/fad7c3203b14e67053e0fc41d8490138b8ff47dd/drm-howto/modeset.c */
23

34
/*
45
* modeset - DRM Modesetting Example

kernel_module/user/m5ops.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#m5ops-instructions */
2+
13
#include <stdint.h>
24
#include <stdio.h>
35
#include <stdlib.h>

kernel_module/user/mmap.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#mmap */
2+
13
#define _XOPEN_SOURCE 700
24
#include <assert.h>
35
#include <fcntl.h>

kernel_module/user/myinsmod.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kernel-module-parameters#myinsmod */
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#myinsmod */
22

33
#define _GNU_SOURCE
44
#include <fcntl.h>

0 commit comments

Comments
 (0)