Skip to content

Commit f2f564a

Browse files
committed
Refine project for further improvements
Originally built to demonstrate Linux kernel facilities like tasklets and kernel threads. Later ported the classical tic-tac-toe game engine as a kernel module, enabling gaming among kernel threads. Now ready to serve as a testbed codebase for further innovations.
1 parent ef02c14 commit f2f564a

15 files changed

+175
-265
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (C) 2021, 2024 National Cheng Kung University, Taiwan.
3+
Copyright (C) 2024-2025 National Cheng Kung University, Taiwan.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

Makefile

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
TARGET = kmldrv
2-
kmldrv-objs = simrupt.o game.o wyhash.o xoroshiro.o mcts.o negamax.o zobrist.o
1+
TARGET = kxo
2+
kxo-objs = main.o game.o xoroshiro.o mcts.o negamax.o zobrist.o
33
obj-m := $(TARGET).o
44

55
ccflags-y := -std=gnu99 -Wno-declaration-after-statement
66
KDIR ?= /lib/modules/$(shell uname -r)/build
77
PWD := $(shell pwd)
88

99
GIT_HOOKS := .git/hooks/applied
10-
all: kmod kmldrv-user
10+
all: kmod xo-user
1111

12-
kmod: $(GIT_HOOKS) simrupt.c
12+
kmod: $(GIT_HOOKS) main.c
1313
$(MAKE) -C $(KDIR) M=$(PWD) modules
1414

15-
kmldrv-user: kmldrv-user.c
15+
xo-user: xo-user.c
1616
$(CC) $(ccflags-y) -o $@ $<
1717

1818
$(GIT_HOOKS):
@@ -22,4 +22,4 @@ $(GIT_HOOKS):
2222

2323
clean:
2424
$(MAKE) -C $(KDIR) M=$(PWD) clean
25-
$(RM) kmldrv-user
25+
$(RM) xo-user

README.md

+32-57
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,47 @@
1-
# KMLdrv: A device driver that performs tic-tac-toe game between kernel threads
1+
# kxo: A Tic-Tac-Toe Game Engine implemented as Linux kernel module
2+
23
## Introduction
3-
`kmldrv` is a simple Linux kernel module which includes the concept of deferred
4-
work and kernel concepts as following:
5-
- circular buffer
6-
- mutex lock
7-
- irq
8-
- softirq
9-
- tasklet
10-
- workqueue
11-
- kernel thread
4+
`kxo` is a Linux kernel module that implements the [tic-tac-toe game](https://en.wikipedia.org/wiki/Tic-tac-toe)
5+
(aka XO Game) as kernel threads.
6+
This educational module demonstrates several essential Linux kernel programming concepts:
7+
- Circular buffer implementation
8+
- Mutex lock synchronization
9+
- IRQ handling
10+
- SoftIRQ processing
11+
- Tasklet scheduling
12+
- Workqueue management
13+
- Kernel thread creation and execution
1214

13-
It can use different machine-learning algorithm and perform tic-tac-toe games between kernel threads. The demo video are in the link below:
14-
- [Kernel space tic-tac-toe game](https://www.youtube.com/watch?v=Y_xdLrDVGzk)
15+
The module supports multiple AI algorithms for game strategy, allowing kernel threads to compete against each other in tic-tac-toe matches.
16+
`kxo` implements two advanced algorithms for tic-tac-toe gameplay:
17+
- Monte Carlo Tree Search (MCTS): A probabilistic algorithm that uses random sampling to evaluate moves and determine optimal game strategies
18+
- Negamax Algorithm: A depth-first minimax variant that efficiently evaluates game positions by alternating between maximizing and minimizing players
1519

16-
## Installation & Usage
17-
You can download the source code via the following command
18-
```
19-
$ git clone https://github.com/vax-r/KMLdrv.git
20-
```
20+
## Build and Run
2121
After the source code is downloaded, go into the directory and do as the following
2222
```
2323
$ make
24-
make -C /lib/modules/6.5.0-28-generic/build M=/home/vax-r/linux2024/KMLdrv modules
25-
make[1]: Entering directory '/usr/src/linux-headers-6.5.0-28-generic'
26-
warning: the compiler differs from the one used to build the kernel
27-
The kernel was built by: x86_64-linux-gnu-gcc-12 (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
28-
You are using: gcc-12 (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
29-
CC [M] /home/vax-r/linux2024/KMLdrv/simrupt.o
30-
CC [M] /home/vax-r/linux2024/KMLdrv/game.o
31-
CC [M] /home/vax-r/linux2024/KMLdrv/wyhash.o
32-
CC [M] /home/vax-r/linux2024/KMLdrv/xoroshiro.o
33-
CC [M] /home/vax-r/linux2024/KMLdrv/mcts.o
34-
CC [M] /home/vax-r/linux2024/KMLdrv/negamax.o
35-
CC [M] /home/vax-r/linux2024/KMLdrv/zobrist.o
36-
LD [M] /home/vax-r/linux2024/KMLdrv/kmldrv.o
37-
MODPOST /home/vax-r/linux2024/KMLdrv/Module.symvers
38-
CC [M] /home/vax-r/linux2024/KMLdrv/kmldrv.mod.o
39-
LD [M] /home/vax-r/linux2024/KMLdrv/kmldrv.ko
40-
BTF [M] /home/vax-r/linux2024/KMLdrv/kmldrv.ko
41-
make[1]: Leaving directory '/usr/src/linux-headers-6.5.0-28-generic'
42-
cc -std=gnu99 -Wno-declaration-after-statement -o kmldrv-user kmldrv-user.c
4324
```
44-
Make sure the kernel object file is compiled correctly, then you can insert the kernel module
25+
26+
Make sure the kernel object file (`kxo.ko`) is built correctly, then you can insert the kernel module
4527
```
46-
$ sudo insmod kmldrv.ko
28+
$ sudo insmod kxo.ko
4729
```
48-
Now you can enjoy the tic-tac-toe games performed between kernel threads through the following command
30+
31+
`kxo` provides an interface for userspace interaction through the companion tool `kxo-user`.
32+
This utility offers the following functionality:
33+
- Display the current status of the `kxo` module (loaded/unloaded)
34+
- Real-time visualization of the tic-tac-toe game board
35+
- Control commands:
36+
- `Ctrl + P`: Toggle pause/resume of the game board display
37+
- `Ctrl + Q`: Terminate all tic-tac-toe games running in kernel space
38+
39+
Simply run the command below after the kernel module is loaded:
4940
```
50-
$ sudo ./kmldrv-user
41+
$ sudo ./kxo-user
5142
```
52-
Enjoy the show !
53-
54-
## Features
55-
### User space tool `kmldrv-user`
56-
`kmldrv` provide a interface for userspace program to interact with it, for example you can use the userspace tool `kmldrv-user`. It has the following ability
57-
- Display the status of `kmldrv`, to show whether its loaded or not
58-
- `Ctrl + P` : Stop/Resume the displaying of chess board
59-
- `Ctrl + Q` : Stop the tic-tac-toe games in kernel space
60-
### Machine Learning Algorithms
61-
Currently `kmldrv` supports two machine learning algorithms
62-
- Monte-Carlo Tree Search
63-
- Negamax AI Algorithm
64-
### PRNG support
65-
Currently `kmldrv` utilize two different PRNG (Pseudo-Random Number Generator) to generate random number
66-
- `xoroshift`
67-
- `wyhash`
6843

6944
## License
7045

71-
`simrupt` is released under the MIT license. Use of this source code is governed
46+
`kxo` is released under the MIT license. Use of this source code is governed
7247
by a MIT-style license that can be found in the LICENSE file.

game.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static char check_line_segment_win(const char *t, int i, int j, line_t line)
2929
return last;
3030
}
3131

32-
char check_win(char *t)
32+
char check_win(const char *t)
3333
{
3434
for (int i_line = 0; i_line < 4; ++i_line) {
3535
line_t line = lines[i_line];

game.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ typedef unsigned fixed_point_t;
3535
extern const line_t lines[4];
3636

3737
int *available_moves(const char *table);
38-
char check_win(char *t);
38+
char check_win(const char *t);
3939
fixed_point_t calculate_win_value(char win, char player);

0 commit comments

Comments
 (0)