标签: kernel

static-key和__builtin_expect有什么区别?

我读了linux内核文件,这个文件描述了static-key:http://lxr.linux.no/linux+v3.11.1/Documentation/static-keys.txt

那么,static-key和__builtin_expect有什么区别?它们都暗示我们可以使用它们来实现likyly()不太可能().

linux kernel linux-kernel

1
推荐指数
1
解决办法
769
查看次数

如何在内核中获取当前用户,即如何以正确的方式调用current-> uid

我正在编写可加载内核模块(LKM).如果刚执行cat/proc /的用户是当前用户,那么这个LKM需要做一些特殊的事情.所以我想弄清楚如何找到这个信息.我的第一次尝试是使用cred.h中定义的get_current_user().但这没有成功.http://lxr.linux.no/linux+*/include/linux/cred.h#L290我不是在寻找其他方法这是我发现的似乎没有工作如何在编写Linux内核模块时获取用户ID 如何做我从内核空间调用Linux系统调用?

有问题的功能:

static int  getuid()
{
 return get_current_user()->uid;
}
Run Code Online (Sandbox Code Playgroud)

gcc输出:

make -C /lib/modules/2.6.32-358.11.1.el6.x86_64/build M=/root/git_prj1 modules
make[1]: Entering directory `/usr/src/kernels/2.6.32-358.11.1.el6.x86_64'
  CC [M]  /root/git_prj1/proc_setup.o
/root/git_prj1/proc_setup.c:37: warning: function declaration isn???t a prototype
/root/git_prj1/proc_setup.c: In function ???getuid???:
/root/git_prj1/proc_setup.c:39: error: dereferencing pointer to incomplete type
/root/git_prj1/proc_setup.c:39: error: implicit declaration of function ???get_uid???
/root/git_prj1/proc_setup.c:39: warning: assignment makes pointer from integer without a cast
/root/git_prj1/proc_setup.c:39: warning: return makes integer from pointer without a cast
/root/git_prj1/proc_setup.c: In function ???read_key???:
/root/git_prj1/proc_setup.c:52: warning: format ???%s??? …
Run Code Online (Sandbox Code Playgroud)

linux kernel linux-kernel procfs

1
推荐指数
1
解决办法
5075
查看次数

为什么在LKM中使用静态函数是一种约定

我最近一直在研究这个问题并且已经查看了各种文章和stackoverflow帖子,但我似乎无法找到一个直接的答案.在创建内核模块时,我看到大多数代码如下所示:

#include <linux/init.h>
static int test_init(void) {return 0;}
static void test_exit(void) {;}
module_init(test_init);
module_exit(test_exit);
Run Code Online (Sandbox Code Playgroud)
  • 我发现的一个可能原因是,这样做会增加将恶意代码注入正在运行的模块的难度.

  • 另一个是命名空间不那么混乱,但这不仅仅是你正在链接和编译的内核模块的上下文中的问题吗?如果insmod实际上将代码链接到内核就像ld一样,那么我可以看到名称冲突如何搞乱系统.这是什么原因?

我想不出任何其他原因,我想在盲目开始使用约定之前澄清这一点.

先感谢您

c linux operating-system kernel kernel-module

1
推荐指数
2
解决办法
140
查看次数

如何在ring0(内核模式)中使用winapi函数?

我不能FileTimeToSystemTime()在驱动程序中调用方法.错误是:

错误C4013:'FileTimeToSystemTime'未定义; 假设extern返回int.

另外我不能包含windows.h因为我已经包含ntddk.h,它会导致很多错误.如果我BOOL FileTimeToSystemTime(IN const PFILETIME, OUT PSYSTEMTIME);在标题中声明它我会得到下一个错误:

错误C2061:语法错误:标识符'FileTimeToSystemTime'

错误C2059:语法错误:';'

错误C2059:语法错误:'type'

错误C4013:'FileTimeToSystemTime'未定义; 假设extern返回int

最后,如果我为此方法编写实现:

BOOL FileTimeToSystemTime(const PFILETIME pFileTime, PSYSTEMTIME pSystemTime)
{
    CALL_ENTRY
    long long tmp;
    memcpy(&tmp, pFileTime, sizeof (FILETIME));
    time_t aTime_t = tmp / 10000;
    tm aTm;

    if (!gmtime_r(&aTime_t, &aTm ))
        return FALSE;

    pSystemTime->wYear = aTm.tm_year + 1900;
    pSystemTime->wMonth = aTm.tm_mon;
    pSystemTime->wDayOfWeek = aTm.tm_wday;
    pSystemTime->wDay = aTm.tm_mday;
    pSystemTime->wHour = aTm.tm_hour;
    pSystemTime->wMinute = aTm.tm_min;
    pSystemTime->wSecond = aTm.tm_sec;
    pSystemTime->wMilliseconds = 0;

    return …
Run Code Online (Sandbox Code Playgroud)

c windows kernel driver

1
推荐指数
1
解决办法
960
查看次数

如何理解内核中"page_align"的宏?

代码是:

#define PAGE_SHIFT      12
#define PAGE_SIZE       (1UL << PAGE_SHIFT)
#define PAGE_MASK       (~(PAGE_SIZE-1))

#define PAGE_ALIGN(addr)        (((addr)+PAGE_SIZE-1)&PAGE_MASK)
Run Code Online (Sandbox Code Playgroud)

我知道这个marco会在页面边界上对齐任何地址.
如何理解这个工具

linux kernel

1
推荐指数
1
解决办法
2385
查看次数

Linux 3.13内核无法编译

在.conf中配置各种选项后,使用

$make config
Run Code Online (Sandbox Code Playgroud)

我尝试使用编译整个Linux内核

$make
Run Code Online (Sandbox Code Playgroud)

但是,它会引发如下错误:

root@localbox:/LinuxKernel/linux-3.13# make
scripts/kconfig/conf --silentoldconfig Kconfig
***
*** Configuration file ".config" not found!
***
*** Please run some configurator (e.g. "make oldconfig" or
*** "make menuconfig" or "make xconfig").
***
make[2]: *** [silentoldconfig] Error 1
make[1]: *** [silentoldconfig] Error 2
make[1]: Nothing to be done for `all'.
make[1]: Nothing to be done for `relocs'.
make: *** No rule to make target `include/config/auto.conf', needed by `include/config/kernel.release'.  Stop.
root@localbox:/LinuxKernel/linux-3.13# 
Run Code Online (Sandbox Code Playgroud)

我试图通过使用以下命令搜索.conf:

$find -name ".conf"
Run Code Online (Sandbox Code Playgroud)

但是没有结果。但是由于make config正常工作,所以我假设.config必须存在于某个地方。

请给我建议如何克服这个烦人的问题。我是第一次这样做,我不确定在尝试编译内核之前是否必须安装任何依赖项。非常感谢您的反馈。

linux kernel compiler-errors makefile compilation

1
推荐指数
1
解决办法
6976
查看次数

linux内核宏如何用作函数?

这样的标题听起来可能很愚蠢,但是我以前从未见过这样的东西,而且我真的不知道该如何描述它:

因此,我只是编写了第一个内核模块,并且使用了一个链表,该链表使用了linux/list.h头文件。其中有一个宏:

400 /**
401  * list_for_each_entry  -       iterate over list of given type
402  * @pos:        the type * to use as a loop cursor.
403  * @head:       the head for your list.
404  * @member:     the name of the list_struct within the struct.
405  */
406 #define list_for_each_entry(pos, head, member)                          \
407         for (pos = list_entry((head)->next, typeof(*pos), member);      \
408              prefetch(pos->member.next), &pos->member != (head);        \
409              pos = list_entry(pos->member.next, typeof(*pos), member))
Run Code Online (Sandbox Code Playgroud)

而且,我用它是这样的(假设list_head是链表的头,并且list是 …

c linux macros kernel kernel-module

1
推荐指数
1
解决办法
2247
查看次数

如何解决这个编译错误

尝试一些FreeBSD内核黑客攻击,我在一个简单的钩子示例中遇到了错误.代码如下

*注意 - 我添加了#include <sys/stat.h>尽可能多的建议,但继续得到相同的错误.

#include <sys/types.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/module.h>
#include <sys/sysent.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/syscall.h>
#include <sys/sysproto.h>
#include <sys/stat.h>

static int mkdir_hook(struct thread *td, void *syscall_args) {

  struct mkdir_args *uap;
  uap = (struct mkdir_args *)syscall_args;
  char path[255];
  size_t done;
  int error;
  error = copyinstr(uap->path, path, 255, &done);
  if(error != 0)
    return (error);

  uprintf("hooked it\n");
  return (mkdir(td, syscall_args));
}

static int load(struct module *module, int cmd, void *arg) {
  int error = 0; …
Run Code Online (Sandbox Code Playgroud)

c kernel freebsd kernel-module

1
推荐指数
1
解决办法
1552
查看次数

内核编程:没有规则来制作目标`-C'

我正在尝试学习内核编程,但在尝试编译一个简单的hello world程序时,我收到以下错误.

make -C /lib/modules/3.2.0-67-generic/build M =/home/arun/KPrograms modules make [1]:输入目录/home/arun/KPrograms' make[1]: *** No rule to make target-C'.停止.make [1]:离开目录`/ home/arun/KPrograms'make:* [all]错误2

我的Makefile

obj?m += hello?1.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)

kernel makefile

1
推荐指数
1
解决办法
2245
查看次数

单片内核的效率

我知道单片内核本身就可以运行所有服务.我在互联网上搜索了为什么每篇文章都说它有效率.我找不到原因.为什么它比其他内核更有效?

architecture linux operating-system kernel process

1
推荐指数
1
解决办法
1092
查看次数