rename include to lkmc

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2019-05-05 00:00:00 +00:00
parent 9c8f95d630
commit 1cc3ee8657
18 changed files with 56 additions and 35 deletions

1
lkmc/README.adoc Normal file
View File

@@ -0,0 +1 @@
https://github.com/cirosantilli/linux-kernel-module-cheat#lkmc-directory

11
lkmc/anonymous_inode.h Normal file
View File

@@ -0,0 +1,11 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#anonymous-inode */
#ifndef LKMC_ANONYMOUS_INODE_H
#define LKMC_ANONYMOUS_INODE_H
#include <linux/ioctl.h>
#define LKMC_ANONYMOUS_INODE_MAGIC 0x33
#define LKMC_ANONYMOUS_INODE_GET_FD _IOR(LKMC_ANONYMOUS_INODE_MAGIC, 0, int)
#endif

44
lkmc/ioctl.h Normal file
View File

@@ -0,0 +1,44 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#ioctl */
#ifndef LKMC_IOCTL_H
#define LKMC_IOCTL_H
#include <linux/ioctl.h>
/* Structs are the way to pass multiple arguments. */
typedef struct {
int i;
int j;
} lkmc_ioctl_struct;
/* TODO some random number I can't understand how to choose. */
#define LKMC_IOCTL_MAGIC 0x33
/* I think those number do not *need* to be unique across, that is just to help debugging:
* https://stackoverflow.com/questions/22496123/what-is-the-meaning-of-this-macro-iormy-macig-0-int
*
* However, the ioctl syscall highjacks several low values at do_vfs_ioctl, e.g.
* This "forces" use to use the _IOx macros...
* https://stackoverflow.com/questions/10071296/ioctl-is-not-called-if-cmd-2
*
* Some of those magic low values are used for fnctl, which can also be used on regular files:
* e.g. FIOCLEX for close-on-exec:
* https://stackoverflow.com/questions/6125068/what-does-the-fd-cloexec-fcntl-flag-do
*
* TODO are the W or R of _IOx and type functional, or only to help with uniqueness?
*
* Documentation/ioctl/ioctl-number.txt documents:
*
* ....
* _IO an ioctl with no parameters
* _IOW an ioctl with write parameters (copy_from_user)
* _IOR an ioctl with read parameters (copy_to_user)
* _IOWR an ioctl with both write and read parameters.
* ....
*/
/* Take an int, increment it. */
#define LKMC_IOCTL_INC _IOWR(LKMC_IOCTL_MAGIC, 0, int)
/* Take a struct with two ints, increment the first, and decrement the second. */
#define LKMC_IOCTL_INC_DEC _IOWR(LKMC_IOCTL_MAGIC, 1, lkmc_ioctl_struct)
#endif

12
lkmc/netlink.h Normal file
View File

@@ -0,0 +1,12 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#netlink-sockets */
#ifndef LKMC_NETLINK_H
#define LKMC_NETLINK_H
/* Socket identifier, matches userland. TODO can be anything?
* Is there a more scalable way to do it? E.g. ioctl device,
* kernel generates one on the fly, then give it back and connect?
* https://stackoverflow.com/questions/32898173/can-i-have-more-than-32-netlink-sockets-in-kernelspace */
#define NETLINK_USER 31
#endif

78
lkmc/ring0.h Normal file
View File

@@ -0,0 +1,78 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#ring0 */
#ifndef LKMC_RING0_H
#define LKMC_RING0_H
#if defined(__x86_64__) || defined(__i386__)
#ifdef THIS_MODULE
#include <linux/kernel.h>
#if defined(__x86_64__)
typedef u64 LkmcRing0RegsType;
#elif defined(__i386__)
typedef u32 LkmcRing0RegsType;
#endif
#else
#include <stdint.h>
#if defined(__x86_64__)
typedef uint64_t LkmcRing0RegsType;
#elif defined(__i386__)
typedef uint32_t LkmcRing0RegsType;
#endif
#endif
typedef struct {
LkmcRing0RegsType cr0;
LkmcRing0RegsType cr2;
LkmcRing0RegsType cr3;
} LkmcRing0Regs;
void lkmc_ring0_get_control_regs(LkmcRing0Regs *ring0_regs) {
#if defined(__x86_64__)
__asm__ __volatile__ (
"mov %%cr0, %%rax;"
"mov %%eax, %[cr0];"
: [cr0] "=m" (ring0_regs->cr0)
:
: "rax"
);
__asm__ __volatile__ (
"mov %%cr2, %%rax;"
"mov %%eax, %[cr2];"
: [cr2] "=m" (ring0_regs->cr2)
:
: "rax"
);
__asm__ __volatile__ (
"mov %%cr3, %%rax;"
"mov %%eax, %[cr3];"
: [cr3] "=m" (ring0_regs->cr3)
:
: "rax"
);
#elif defined(__i386__)
__asm__ __volatile__ (
"mov %%cr0, %%eax;"
"mov %%eax, %[cr0];"
: [cr0] "=m" (ring0_regs->cr0)
:
: "eax"
);
__asm__ __volatile__ (
"mov %%cr2, %%eax;"
"mov %%eax, %[cr2];"
: [cr2] "=m" (ring0_regs->cr2)
:
: "eax"
);
__asm__ __volatile__ (
"mov %%cr3, %%eax;"
"mov %%eax, %[cr3];"
: [cr3] "=m" (ring0_regs->cr3)
:
: "eax"
);
#endif
}
#endif