mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-28 12:34:26 +01:00
kernel module: format, use tab to indent.
This commit is contained in:
committed by
Ciro Santilli 包子露宪 六四事件 法轮功
parent
9f6ddbc436
commit
3b0a343647
@@ -33,42 +33,42 @@ static const struct file_operations fops = {
|
|||||||
|
|
||||||
static void cleanup(int device_created)
|
static void cleanup(int device_created)
|
||||||
{
|
{
|
||||||
if (device_created) {
|
if (device_created) {
|
||||||
device_destroy(myclass, major);
|
device_destroy(myclass, major);
|
||||||
cdev_del(&mycdev);
|
cdev_del(&mycdev);
|
||||||
}
|
}
|
||||||
if (myclass)
|
if (myclass)
|
||||||
class_destroy(myclass);
|
class_destroy(myclass);
|
||||||
if (major != -1)
|
if (major != -1)
|
||||||
unregister_chrdev_region(major, 1);
|
unregister_chrdev_region(major, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int myinit(void)
|
static int myinit(void)
|
||||||
{
|
{
|
||||||
int device_created = 0;
|
int device_created = 0;
|
||||||
|
|
||||||
/* cat /proc/devices */
|
/* cat /proc/devices */
|
||||||
if (alloc_chrdev_region(&major, 0, 1, NAME "_proc") < 0)
|
if (alloc_chrdev_region(&major, 0, 1, NAME "_proc") < 0)
|
||||||
goto error;
|
goto error;
|
||||||
/* ls /sys/class */
|
/* ls /sys/class */
|
||||||
if ((myclass = class_create(THIS_MODULE, NAME "_sys")) == NULL)
|
if ((myclass = class_create(THIS_MODULE, NAME "_sys")) == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
/* ls /dev/ */
|
/* ls /dev/ */
|
||||||
if (device_create(myclass, NULL, major, NULL, NAME "_dev") == NULL)
|
if (device_create(myclass, NULL, major, NULL, NAME "_dev") == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
device_created = 1;
|
device_created = 1;
|
||||||
cdev_init(&mycdev, &fops);
|
cdev_init(&mycdev, &fops);
|
||||||
if (cdev_add(&mycdev, major, 1) == -1)
|
if (cdev_add(&mycdev, major, 1) == -1)
|
||||||
goto error;
|
goto error;
|
||||||
return 0;
|
return 0;
|
||||||
error:
|
error:
|
||||||
cleanup(device_created);
|
cleanup(device_created);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void myexit(void)
|
static void myexit(void)
|
||||||
{
|
{
|
||||||
cleanup(1);
|
cleanup(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(myinit)
|
module_init(myinit)
|
||||||
|
|||||||
@@ -8,8 +8,8 @@
|
|||||||
static int myinit(void)
|
static int myinit(void)
|
||||||
{
|
{
|
||||||
void *dst, *src;
|
void *dst, *src;
|
||||||
dst = kmalloc(0x10, GFP_KERNEL);
|
dst = kmalloc(0x10, GFP_KERNEL);
|
||||||
src = kmalloc(0x1000000, GFP_KERNEL);
|
src = kmalloc(0x1000000, GFP_KERNEL);
|
||||||
memcpy(dst, src, 0x1000000);
|
memcpy(dst, src, 0x1000000);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ static void vm_close(struct vm_area_struct *vma)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* First page access. */
|
/* First page access. */
|
||||||
int (*fault)(struct vm_fault *vmf);
|
int (*fault)(struct vm_fault *vmf);
|
||||||
static int vm_fault(struct vm_fault *vmf)
|
static int vm_fault(struct vm_fault *vmf)
|
||||||
{
|
{
|
||||||
struct page *page;
|
struct page *page;
|
||||||
@@ -79,13 +79,13 @@ static int open(struct inode *inode, struct file *filp)
|
|||||||
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
|
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
|
||||||
{
|
{
|
||||||
struct mmap_info *info;
|
struct mmap_info *info;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
pr_info("read\n");
|
pr_info("read\n");
|
||||||
info = filp->private_data;
|
info = filp->private_data;
|
||||||
ret = min(len, (size_t)BUFFER_SIZE);
|
ret = min(len, (size_t)BUFFER_SIZE);
|
||||||
if (copy_to_user(buf, info->data, ret)) {
|
if (copy_to_user(buf, info->data, ret)) {
|
||||||
ret = -EFAULT;
|
ret = -EFAULT;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -96,11 +96,11 @@ static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff
|
|||||||
|
|
||||||
pr_info("write\n");
|
pr_info("write\n");
|
||||||
info = filp->private_data;
|
info = filp->private_data;
|
||||||
if (copy_from_user(info->data, buf, min(len, (size_t)BUFFER_SIZE))) {
|
if (copy_from_user(info->data, buf, min(len, (size_t)BUFFER_SIZE))) {
|
||||||
return -EFAULT;
|
return -EFAULT;
|
||||||
} else {
|
} else {
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int release(struct inode *inode, struct file *filp)
|
static int release(struct inode *inode, struct file *filp)
|
||||||
|
|||||||
@@ -18,47 +18,47 @@ static void callback(struct sk_buff *skb)
|
|||||||
{
|
{
|
||||||
char readbuf[9];
|
char readbuf[9];
|
||||||
size_t readbuflen;
|
size_t readbuflen;
|
||||||
int pid;
|
int pid;
|
||||||
int res;
|
int res;
|
||||||
struct nlmsghdr *nlh;
|
struct nlmsghdr *nlh;
|
||||||
struct sk_buff *skb_out;
|
struct sk_buff *skb_out;
|
||||||
|
|
||||||
nlh = (struct nlmsghdr *)skb->data;
|
nlh = (struct nlmsghdr *)skb->data;
|
||||||
pr_info("kernel received: %s\n", (char *)nlmsg_data(nlh));
|
pr_info("kernel received: %s\n", (char *)nlmsg_data(nlh));
|
||||||
if (sleep)
|
if (sleep)
|
||||||
usleep_range(1000000, 1000001);
|
usleep_range(1000000, 1000001);
|
||||||
readbuflen = snprintf(readbuf, sizeof(readbuf), "%x", count);
|
readbuflen = snprintf(readbuf, sizeof(readbuf), "%x", count);
|
||||||
count++;
|
count++;
|
||||||
pid = nlh->nlmsg_pid;
|
pid = nlh->nlmsg_pid;
|
||||||
skb_out = nlmsg_new(readbuflen, 0);
|
skb_out = nlmsg_new(readbuflen, 0);
|
||||||
if (!skb_out) {
|
if (!skb_out) {
|
||||||
pr_err("nlmsg_new\n");
|
pr_err("nlmsg_new\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, readbuflen, 0);
|
nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, readbuflen, 0);
|
||||||
NETLINK_CB(skb_out).dst_group = 0;
|
NETLINK_CB(skb_out).dst_group = 0;
|
||||||
strncpy(nlmsg_data(nlh), readbuf, readbuflen);
|
strncpy(nlmsg_data(nlh), readbuf, readbuflen);
|
||||||
res = nlmsg_unicast(nl_sk, skb_out, pid);
|
res = nlmsg_unicast(nl_sk, skb_out, pid);
|
||||||
if (res < 0)
|
if (res < 0)
|
||||||
pr_info("nlmsg_unicast\n");
|
pr_info("nlmsg_unicast\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int myinit(void)
|
static int myinit(void)
|
||||||
{
|
{
|
||||||
struct netlink_kernel_cfg cfg = {
|
struct netlink_kernel_cfg cfg = {
|
||||||
.input = callback,
|
.input = callback,
|
||||||
};
|
};
|
||||||
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
|
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
|
||||||
if (!nl_sk) {
|
if (!nl_sk) {
|
||||||
pr_err("netlink_kernel_create\n");
|
pr_err("netlink_kernel_create\n");
|
||||||
return -10;
|
return -10;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void myexit(void)
|
static void myexit(void)
|
||||||
{
|
{
|
||||||
netlink_kernel_release(nl_sk);
|
netlink_kernel_release(nl_sk);
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(myinit);
|
module_init(myinit);
|
||||||
|
|||||||
@@ -15,75 +15,75 @@
|
|||||||
#define QEMU_VENDOR_ID 0x1234
|
#define QEMU_VENDOR_ID 0x1234
|
||||||
|
|
||||||
static struct pci_device_id id_table[] = {
|
static struct pci_device_id id_table[] = {
|
||||||
{ PCI_DEVICE(QEMU_VENDOR_ID, EDU_DEVICE_ID), },
|
{ PCI_DEVICE(QEMU_VENDOR_ID, EDU_DEVICE_ID), },
|
||||||
{ 0, }
|
{ 0, }
|
||||||
};
|
};
|
||||||
MODULE_DEVICE_TABLE(pci, id_table);
|
MODULE_DEVICE_TABLE(pci, id_table);
|
||||||
static int major;
|
static int major;
|
||||||
static struct pci_dev *pdev;
|
static struct pci_dev *pdev;
|
||||||
static void __iomem *mmio;
|
static void __iomem *mmio;
|
||||||
static struct file_operations fops = {
|
static struct file_operations fops = {
|
||||||
.owner = THIS_MODULE,
|
.owner = THIS_MODULE,
|
||||||
};
|
};
|
||||||
|
|
||||||
static irqreturn_t irq_handler(int irq, void *dev)
|
static irqreturn_t irq_handler(int irq, void *dev)
|
||||||
{
|
{
|
||||||
pr_info("irq_handler irq = %d dev = %d\n", irq, *(int *)dev);
|
pr_info("irq_handler irq = %d dev = %d\n", irq, *(int *)dev);
|
||||||
iowrite32(0, mmio + 4);
|
iowrite32(0, mmio + 4);
|
||||||
return IRQ_HANDLED;
|
return IRQ_HANDLED;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int probe(struct pci_dev *dev, const struct pci_device_id *id)
|
static int probe(struct pci_dev *dev, const struct pci_device_id *id)
|
||||||
{
|
{
|
||||||
pr_info("probe\n");
|
pr_info("probe\n");
|
||||||
major = register_chrdev(0, CDEV_NAME, &fops);
|
major = register_chrdev(0, CDEV_NAME, &fops);
|
||||||
pdev = dev;
|
pdev = dev;
|
||||||
if (pci_enable_device(dev) < 0) {
|
if (pci_enable_device(dev) < 0) {
|
||||||
dev_err(&(pdev->dev), "pci_enable_device\n");
|
dev_err(&(pdev->dev), "pci_enable_device\n");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if (pci_request_region(dev, BAR, "myregion0")) {
|
if (pci_request_region(dev, BAR, "myregion0")) {
|
||||||
dev_err(&(pdev->dev), "pci_request_region\n");
|
dev_err(&(pdev->dev), "pci_request_region\n");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
mmio = pci_iomap(pdev, BAR, pci_resource_len(pdev, BAR));
|
mmio = pci_iomap(pdev, BAR, pci_resource_len(pdev, BAR));
|
||||||
pr_info("dev->irq = %u\n", dev->irq);
|
pr_info("dev->irq = %u\n", dev->irq);
|
||||||
if (request_irq(dev->irq, irq_handler, IRQF_SHARED, "pci_irq_handler0", &major) < 0) {
|
if (request_irq(dev->irq, irq_handler, IRQF_SHARED, "pci_irq_handler0", &major) < 0) {
|
||||||
dev_err(&(dev->dev), "request_irq\n");
|
dev_err(&(dev->dev), "request_irq\n");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
iowrite32(0x12345678, mmio);
|
iowrite32(0x12345678, mmio);
|
||||||
return 0;
|
return 0;
|
||||||
error:
|
error:
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void remove(struct pci_dev *dev)
|
static void remove(struct pci_dev *dev)
|
||||||
{
|
{
|
||||||
pr_info("remove\n");
|
pr_info("remove\n");
|
||||||
free_irq(dev->irq, &major);
|
free_irq(dev->irq, &major);
|
||||||
pci_release_region(dev, BAR);
|
pci_release_region(dev, BAR);
|
||||||
unregister_chrdev(major, CDEV_NAME);
|
unregister_chrdev(major, CDEV_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct pci_driver pci_driver = {
|
static struct pci_driver pci_driver = {
|
||||||
.name = CDEV_NAME,
|
.name = CDEV_NAME,
|
||||||
.id_table = id_table,
|
.id_table = id_table,
|
||||||
.probe = probe,
|
.probe = probe,
|
||||||
.remove = remove,
|
.remove = remove,
|
||||||
};
|
};
|
||||||
|
|
||||||
static int myinit(void)
|
static int myinit(void)
|
||||||
{
|
{
|
||||||
if (pci_register_driver(&pci_driver) < 0) {
|
if (pci_register_driver(&pci_driver) < 0) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void myexit(void)
|
static void myexit(void)
|
||||||
{
|
{
|
||||||
pci_unregister_driver(&pci_driver);
|
pci_unregister_driver(&pci_driver);
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(myinit);
|
module_init(myinit);
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ static int myinit(void)
|
|||||||
pr_info("cr2 = 0x%8.8llX\n", (unsigned long long)ring0_regs.cr2);
|
pr_info("cr2 = 0x%8.8llX\n", (unsigned long long)ring0_regs.cr2);
|
||||||
pr_info("cr3 = 0x%8.8llX\n", (unsigned long long)ring0_regs.cr3);
|
pr_info("cr3 = 0x%8.8llX\n", (unsigned long long)ring0_regs.cr3);
|
||||||
#endif
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void myexit(void) {}
|
static void myexit(void) {}
|
||||||
|
|||||||
@@ -72,24 +72,24 @@ static int show(struct seq_file *s, void *v)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static struct seq_operations my_seq_ops = {
|
static struct seq_operations my_seq_ops = {
|
||||||
.next = next,
|
.next = next,
|
||||||
.show = show,
|
.show = show,
|
||||||
.start = start,
|
.start = start,
|
||||||
.stop = stop,
|
.stop = stop,
|
||||||
};
|
};
|
||||||
|
|
||||||
static int open(struct inode *inode, struct file *file)
|
static int open(struct inode *inode, struct file *file)
|
||||||
{
|
{
|
||||||
pr_info("open\n");
|
pr_info("open\n");
|
||||||
return seq_open(file, &my_seq_ops);
|
return seq_open(file, &my_seq_ops);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct file_operations fops = {
|
static struct file_operations fops = {
|
||||||
.owner = THIS_MODULE,
|
.owner = THIS_MODULE,
|
||||||
.llseek = seq_lseek,
|
.llseek = seq_lseek,
|
||||||
.open = open,
|
.open = open,
|
||||||
.read = seq_read,
|
.read = seq_read,
|
||||||
.release = seq_release
|
.release = seq_release
|
||||||
};
|
};
|
||||||
|
|
||||||
static int myinit(void)
|
static int myinit(void)
|
||||||
|
|||||||
Reference in New Issue
Block a user