我正在按照以下教程,尝试学习如何开发设备驱动程序,在第2章中,重点是开发一个工作模块并将其插入到内核中.我使用了以下代码(hello.c):
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello World!\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world!\n");
}
module_init(hello_init);
module_exit(hello_exit);
Run Code Online (Sandbox Code Playgroud)
我的这是Makefile:
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Run Code Online (Sandbox Code Playgroud)
然后我在LXTerminal中运行以下命令:
brian@brian-desktop:~/driver_stuff/hello$ su
root@brian-desktop:/home/brian/driver_stuff/hello# make
make -C /lib/modules/2.6.32-21-generic/build M=/home/brian/driver_stuff/hello modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.32-21-generic'
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-21-generic'
root@brian-desktop:/home/brian/driver_stuff/hello# insmod ./hello.ko …Run Code Online (Sandbox Code Playgroud) 我想知道我是否可以同时进行大量的系统调用,只需要一个交换机开销.我需要这个,因为我需要同时进行许多(128)系统调用.如果我能做到这一点而不在内核和用户之间切换256次以上,我认为它可以使我的(速度敏感)库明显更快.
当这样定义时t_ioctl,我没有得到警告:
long t_ioctl(struct file *filep, unsigned int cmd, unsigned long input){
Run Code Online (Sandbox Code Playgroud)
定义时t_ioctl如下:
static long t_ioctl(struct file *filep, unsigned int cmd, unsigned long input){
Run Code Online (Sandbox Code Playgroud)
我收到警告:
warning: 't_ioctl' defined but not used
Run Code Online (Sandbox Code Playgroud)
但是当它达到t_read或t_write静态和非静态函数声明不会导致警告.例如:
static ssize_t t_read(struct file *filp, char __user * buf, size_t count, loff_t * f_pos);
Run Code Online (Sandbox Code Playgroud)
为什么我在一个案例而不是另一个案件中得到警告?
我在一个驱动程序中声明了一个静态const int变量并导出了该变量符号.在另一个驱动程序中我正在修改该变量.另一个驱动程序打印修改后的值,但原始驱动程序保留原始值.当两个驱动程序看到的变量的虚拟和物理地址都相同时,这怎么可能.这是一个内核bug吗?
[root@localhost bug]# uname -a
Linux localhost.localdomain 3.5.3 #3 SMP Mon Sep 3 21:52:12 IST 2012
i686 i686 i386 GNU/Linux
Run Code Online (Sandbox Code Playgroud)
[root @ localhost bug] #cat driver.c
#include <linux/module.h>
static const int value = 123;
int init_module()
{
printk("Base driver (init): value = %d\n", value);
printk("Base driver (init): virtual address of value = %p\n", (void *)&value);
printk("Base driver (init): physical address of value = %p\n", (void
*)__pa(&value));
return 0;
}
void cleanup_module()
{
printk("Base driver (exit): value = %d\n", value); …Run Code Online (Sandbox Code Playgroud) 我面临着一个奇怪的问题.相同文本文件的md5sum在windows和linux上是不同的.我希望它是相同的,因为哈希是使用文件中存在的内容生成的,并且不依赖于操作系统.是否有任何具体原因发生这样的事情?sha1sum也一样吗?
我实施了一个新的系统调用作为介绍练习.它只需要一个缓冲区和printk缓冲区.我后来才知道正确的做法是使用copy_from_user.
这只是一个验证地址的预防措施,还是我的系统调用导致一些我看不到的错误(页面错误?)?
如果只是一种预防措施,它有什么保护作用?
谢谢!
我对Linux内核代码中使用的语法有疑问.我对它的作用有直觉,但我想更正式地了解它.我正在使用内核v3.5.4
在文件/include/linux/sched.h中定义了以下内容
struct task_struct {
volatile long state;
//some more data members
};
Run Code Online (Sandbox Code Playgroud)
并在文件/include/linux/init_task.h文件中定义以下内容:
#define INIT_TASK(tsk) {
.state = 0, \
//some more initializations
}
Run Code Online (Sandbox Code Playgroud)
我对两件事感到困惑:
a)我觉得它用于初始化,但是任何人都可以为这种类型的结构初始化提出一些好的读数.
b)我不明白以下初始化是如何工作的.就像这个#define和相应的task_struct结构是如何相关的.
[编辑]我也注意到以下事项:c)是否\在必要的每一行的末尾.
d)内核doe的许多部分都包含在内#ifdef #endif.如果要初始化包含在其中的数据成员,#ifdef #endif我们可以使用这种形式的初始化.我的意思是我们可以像这样使用#ifdef #endif内部INIT_TASK()
#define INIT_TASK(tsk) {
.state = 0, \
//some more initializations
#ifdef CX
.tickets = 5, \
#endif
}
Run Code Online (Sandbox Code Playgroud) 我无法猜测如何使用构造
struct
{
uint64_t offsets[0];
} table;
Run Code Online (Sandbox Code Playgroud)
请给我一些暗示.
我正在浏览linux内核源代码并找到了这个函数定义.
function(struct net * const *pnet)
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下这意味着什么.它是净*或净**还是常网*或常网**?
谢谢
我试图找到像.pdf,.doc,.docx等文件的文件类型,但编程不使用shell命令.实际上我必须创建一个阻止访问特定扩展名文件的应用程序.我在LKM已经上瘾sys_call_table的,现在我想,当触发打开/读取系统调用,然后我的LKM检查文件类型.
我知道我们有一个当前指针可以访问当前的进程结构,我们可以使用它来查找存储在dentry结构中的文件名,在Linux中,文件类型由存储在文件起始字节中的幻数标识.但我不知道如何查找文件类型及其存储位置?
linux-kernel ×10
c ×8
linux ×5
kernel ×3
const ×1
debian ×1
module ×1
printk ×1
system-calls ×1
windows ×1