kernel/module.c
#include <linux/errno.h>
#include <linux/kernel.h>
#include <asm/segment.h>
#include <linux/mm.h> /* defines GFP_KERNEL */
#include <linux/string.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/malloc.h>
struct module *module_list = NULL;
int freeing_modules; /* true if some modules are marked for deletion */
struct module *find_module( const char *name);
int get_mod_name( char *user_name, char *buf);
int free_modules( void);
/*
* Allocate space for a module.
*/
//系统调用,创建module,为module分配空间
asmlinkage int
sys_create_module(char *module_name, unsigned long size)
{
int npages;
void* addr;
int len;
char name[MOD_MAX_NAME];
char *savename;
struct module *mp;
int error;
//如果不是超级用户root,则退出
if (!suser())
return -EPERM;
//如果模块的名称为空或者长度为0,则退出
if (module_name == NULL || size == 0)
return -EINVAL;
//根据模块的名称,获取name
if ((error = get_mod_name(module_name, name)) != 0)
return error;
//搜索module
if (find_module(name) != NULL) {
return -EEXIST;
}
len = strlen(name) + 1;
//分配空间
if ((savename = (char*) kmalloc(len, GFP_KERNEL)) == NULL)
return -ENOMEM;
//拷贝名称
memcpy(savename, name, len);
//申请空间
if ((mp = (struct module*) kmalloc(sizeof *mp, GFP_KERNEL)) == NULL) {
kfree(savename);
return -ENOMEM;
}
//计算申请的页数
npages = (size + sizeof (int) + 4095) / 4096;
if ((addr = vmalloc(npages * 4096)) == 0) {
kfree_s(mp, sizeof *mp);
kfree(savename);
return -ENOMEM;
}
mp->name = savename;
mp->size = npages;
mp->addr = addr;
mp->state = MOD_UNINITIALIZED;
* (int *) addr = 0; /* set use count to zero */
mp->cleanup = NULL;
mp->next = module_list;
module_list = mp;
printk("module `%s' (%lu pages @ 0x%08lx) created\n",
mp->name, (unsigned long) mp->size, (unsigned long) mp->addr);
return (int) addr;
}
/*
* Initialize a module.
*/
//初始化module
asmlinkage int
sys_init_module(char *module_name, char *code, unsigned codesize,
struct mod_routines *routines)
{
struct module *mp;
char name[MOD_MAX_NAME];
int error;
struct mod_routines rt;
if (!suser())
return -EPERM;
/*
* First reclaim any memory from dead modules that where not
* freed when deleted. Should I think be done by timers when
* the module was deleted - Jon.
*/
free_modules();
if ((error = get_mod_name(module_name, name)) != 0)
return error;
printk( "initializing module `%s', %d (0x%x) bytes\n",
name, codesize, codesize);
memcpy_fromfs(&rt, routines, sizeof rt);
if ((mp = find_module(name)) == NULL)
return -ENOENT;
if ((codesize + sizeof (int) + 4095) / 4096 > mp->size)
return -EINVAL;
memcpy_fromfs((char *)mp->addr + sizeof (int), code, codesize);
memset((char *)mp->addr + sizeof (int) + codesize, 0,
mp->size * 4096 - (codesize + sizeof (int)));
printk( " init entry @ 0x%08lx, cleanup entry @ 0x%08lx\n",
(unsigned long) rt.init, (unsigned long) rt.cleanup);
mp->cleanup = rt.cleanup;
if ((*rt.init)() != 0)
return -EBUSY;
mp->state = MOD_RUNNING;
return 0;
}
//删除module
asmlinkage int
sys_delete_module(char *module_name)
{
struct module *mp;
char name[MOD_MAX_NAME];
int error;
if (!suser())
return -EPERM;
if (module_name != NULL) {
if ((error = get_mod_name(module_name, name)) != 0)
return error;
if ((mp = find_module(name)) == NULL)
return -ENOENT;
if (mp->state == MOD_RUNNING)
(*mp->cleanup)();
mp->state = MOD_DELETED;
}
free_modules();
return 0;
}
/*
* Copy the kernel symbol table to user space. If the argument is null,
* just return the size of the table.
*/
//获取内核符号表
asmlinkage int
sys_get_kernel_syms(struct kernel_sym *table)
{
struct symbol {
unsigned long addr;
char *name;
};
extern int symbol_table_size;
extern struct symbol symbol_table[];
int i;
struct symbol *from;
struct kernel_sym *to;
struct kernel_sym sym;
if (table != NULL) {
from = symbol_table;
to = table;
i = verify_area(VERIFY_WRITE, to, symbol_table_size * sizeof *table);
if (i)
return i;
for (i = symbol_table_size ; --i >= 0 ; ) {
sym.value = from->addr;
strncpy(sym.name, from->name, sizeof sym.name);
memcpy_tofs(to, &sym, sizeof sym);
from++, to++;
}
}
return symbol_table_size;
}
/*
* Copy the name of a module from user space.
*/
//获取mod的名称
int
get_mod_name(char *user_name, char *buf)
{
int i;
i = 0;
for (i = 0 ; (buf[i] = get_fs_byte(user_name + i)) != '\0' ; ) {
if (++i >= MOD_MAX_NAME)
return -E2BIG;
}
return 0;
}
/*
* Look for a module by name, ignoring modules marked for deletion.
*/
//搜索module
struct module *
find_module( const char *name)
{
struct module *mp;
for (mp = module_list ; mp ; mp = mp->next) {
if (mp->state == MOD_DELETED)
continue;
if (!strcmp(mp->name, name))
break;
}
return mp;
}
/*
* Try to free modules which have been marked for deletion. Returns nonzero
* if a module was actually freed.
*/
//释放module
int
free_modules( void)
{
struct module *mp;
struct module **mpp;
int did_deletion;
did_deletion = 0;
freeing_modules = 0;
mpp = &module_list;
while ((mp = *mpp) != NULL) {
if (mp->state != MOD_DELETED) {
mpp = &mp->next;
} else if (GET_USE_COUNT(mp) != 0) {
freeing_modules = 1;
mpp = &mp->next;
} else { /* delete it */
*mpp = mp->next;
vfree(mp->addr);
kfree(mp->name);
kfree_s(mp, sizeof *mp);
did_deletion = 1;
}
}
return did_deletion;
}
/*
* Called by the /proc file system to return a current list of modules.
*/
//获取所有的module
int get_module_list(char *buf)
{
char *p;
char *q;
int i;
struct module *mp;
char size[32];
p = buf;
for (mp = module_list ; mp ; mp = mp->next) {
if (p - buf > 4096 - 100)
break; /* avoid overflowing buffer */
q = mp->name;
i = 20;
while (*q) {
*p++ = *q++;
i--;
}
sprintf(size, "%d", mp->size);
i -= strlen(size);
if (i <= 0)
i = 1;
while (--i >= 0)
*p++ = ' ';
q = size;
while (*q)
*p++ = *q++;
if (mp->state == MOD_UNINITIALIZED)
q = " (uninitialized)";
else if (mp->state == MOD_RUNNING)
q = "";
else if (mp->state == MOD_DELETED)
q = " (deleted)";
else
q = " (bad state)";
while (*q)
*p++ = *q++;
*p++ = '\n';
}
return p - buf;
}
kernel/module.c的更多相关文章
- The vboxdrv kernel module is not loaded
背景: 在没有关虚拟机的情况下, 直接关了电脑, 我的电脑系统是Centos 6 错误的提示: 在终端执行virtualbox -v 时提示 The vboxdrv kernel module is ...
- Xamarin.Android模拟器提示HAX kernel module is not Installed
Xamarin.Android模拟器提示HAX kernel module is not Installed 错误信息:emulator : ERROR : x86 emulation current ...
- HAX kernel module is not installed
dev.android.emulator.haxm 运行emulator -avd xxx来启动名为xxx的模拟器,但报如下错误: emulator: ERROR: x86 emulation cur ...
- emulator: ERROR: x86 emulation currently requires hardware acceleration!Please ensure Intel HAXM is properly installed and usable.CPU acceleration status: HAX kernel module is not installed!
Android Studio 1.0 已经放出来了,以后的Android平台开发激昂逐步从Eclipse向Android Studio迁移,为了能不落伍我也特意从Google下载了Android St ...
- Compiling a kernel module for the raspberry pi 2 via Ubuntu host
Compiling a kernel module for the raspberry pi 2 via Ubuntu host Normally compiling a kernel module ...
- Virtualbox报错------>make sure the kernel module has been loaded successfully
错误描述 很久没有用virtualbox了,今天打算在virtualbox上安装一个Ubuntu系统的时候,新建好Ubuntu后启动的时候,直接报错: Cannot access the kernel ...
- qemu 出现Could not access KVM kernel module: No such file or directory failed to initialize KVM: No such file or directory
使用qemu命令 qemu-system-x86_64 -hda image/ubuntu-test.img -cdrom ubuntu-16.04.2-server-amd64.iso -m 102 ...
- 如何处理VirtualBox启动错误消息:The vboxdrv kernel module is not loaded
我在启动minikube时,遇到如下错误消息: Starting local Kubernetes v1.10.0 cluster... Starting VM... E1010 03:27:37.9 ...
- OpenWrt Kernel Module Creation Howto
OpenWrt Kernel Module Creation Howto About OpenWrt Kernel Module Compilation You are planning to com ...
随机推荐
- js中的正则表达式
一.正则中的汉字 常见的:/[^\x00-\x7F]+?/ /^[\u2E80-\u9FFF]+$/ 过滤汉字即是:string.replace(/^[\u2E80-\u9FFF]+$/g, &quo ...
- linux chomd 学习
chomd -R 777 directory_name :递归地给directory目录下所有文件和子目录的属主分配读的权限 ------2016-10-31 -- source: Linux chm ...
- 32位和64位系统区别及int字节数
理论上来讲 我觉得数据类型的字节数应该是由CPU决定的,但是实际上主要由编译器决定(占多少位由编译器在编译期间说了算). 常用数据类型对应字节数 可用如sizeof(char),sizeof(ch ...
- poj2280Amphiphilic Carbon Molecules(极角排序)
链接 卡了几天的破题,对于hdu的那份数据,这就一神题.. 借助极角排序,枚举以每一个点进行极角排序,然后构造两条扫描线,一个上面一个下面,两条同时走,把上线和下线的点以及上线左边的点分别统计出来,如 ...
- 入坑HttpServletRequest.getParameterMap
在项目开发的时候遇到一个小坑,在发送了异步请求以后,回调的时候传递给我一个参数直接就是HttpServletRequest的请求,下面简称request: 在使用的时候自以为很简单,直接get就好了嘛 ...
- Sealed密封类
using System; using System.Collections.Generic; using System.Linq; using System.Text; #region 概述 //在 ...
- update
update `表名` set 字段名 =replace(字段名, '查找的内容','更改的内容') where 字段名 like '%查找的内容%'; update shangpin set cli ...
- centos7中 npm install express 时Error: Cannot find module 'express'错误
费了很大劲最后在网上找到,在自己的工程目录下再次执行npm install express搞定.
- 【转】 CPU大小端
大端模式,是指数据的低位保存在内存的高地址中,而数据的高位,保存在内存的低地址中:小端模式,是指数据的低位保存在内存的低地址中,而数据的高位保存在内存的高地址中. 为什么会有大小端模式之分呢?这是因为 ...
- Xcode创建Object-C程序
一. Xcode 环境安装 与 工程创建 1. 下载环境 相关资源下载 : -- IOS 相关资料下载页面 : https://developer.apple.com/devcenter/ios/i ...