Skip to content

Instantly share code, notes, and snippets.

@kimhoki
Last active December 6, 2020 16:56
Show Gist options
  • Save kimhoki/9bc3b74ae6225ac3c557 to your computer and use it in GitHub Desktop.
Save kimhoki/9bc3b74ae6225ac3c557 to your computer and use it in GitHub Desktop.
[Device Driver] param , PARM, 커널 모듈 매개 변수 매크로

module_param_named, 커널 모듈 매개 변수 매크로

모듈 매개 변수 지정 방법:

#include:

#include <linux/moduleparam.h>
변수 타입 Value
short short
int int
long long
charp char*
invbool int
ushort unsigned short
uint unsigned int
ulong unsigned long
bool int
intarray int*

접근 속성 보통 0으로 지정하며, 사용자 권한에 따라 처리 허가 여부를 검사하려 할 때


**모듈 매개 변수 매크로 **

커널은 사용자가 매개변수를 부팅할때나 모듈 로드 시에 선언하고 드라이버 안에서는 이 매개변수들이 전역 변수처럼 보이도록 하는 프레임 웍을 제공한다.

module_param()

module_param_named()

module_param_string()

module_param_array()

module_param_array_mamed()


**매크로 종류 **

##module_param(name, type, perm)

Command Line 인자를 얻음

EX) ./insmod mymodule.ko myvariable = 5

int myint = 3;

##module_param_array(name, type, nump, perm)

변수 명, 자료형, nump는 정수형 포인터, sysfs의 접근 권한

세번째 인자로 배열 개수를 포인터로 넘겨줌

세 번째 인자로 배열 개수 대신 NULL을 넘겨 주어도 됨

int myintarray[2];
module_param_array(myintarray,int,NULL,0)
int myshortarray[4];
int count;
module_param_array(myshortarray, int, &count, 0);

##module_param_string(name, string, len, perm);

charp로 정의된 문자열을 지정할 때, len으로 길이와 함께 지정

길이는 sizeof(string) 형태로 사용

##module_param_named(name, value, type, perm);

변수의 값(value)를 내부에 포함 시키고자 할 때 사용


##MODULE_PARM_DESC()

모듈이 가질 수 있는 문서 인자들로 사용

두 개의 인자를 가짐

변수 이름, 변수를 묘사하는 문자열

int myintarray[2];
module_param_array(myintarray,int,NULL,0)
int myshortarray[4];
int count;
module_param_array(myshortarray, int, &count, 0);

첨부 코드

  • Makefile.c
  • module_param.c

Makefile:

KERNELDIR =	/lib/modules/$(shell uname -r)/build
obj-m	:= module_param.o
KDIR	:=	/lib/modules/$(shell uname -r)/build
PWD	:=	$(shell pwd)
default:
	$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
	rm	-rf *.ko
	rm	-rf *.mod.*
	rm	-rf .*.cmd
	rm	-rf *.o
	rm	-rf .tmp_versions

module_param.c:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/moduleparam.h>
#include <linux/stat.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Device Driver");
MODULE_DESCRIPTION("Parameter Test Module");


static short int myshort = 1;
static       int myint   = 420;
static long  int mylong  = 9999;
static char    *mystring = "mykernel";
static int      arr_argc = 0;

module_param(myshort, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(myshort, "A short integer");

module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(myint, "An integer");

module_param(mylong, long, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(mylong, "A long integer");

module_param(mystring, charp, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(mystring, "A character string");

static int __init hello_param_init(void)
{
	int i;
	printk("Hello, world \n");
	printk("myshort short integer: %hd\n", myshort);
	printk("myint is an integer: %d\n", myint);
	printk("mylong long integer: %ld\n", mylong);
	printk("myshort string is a string: %s\n", mystring);
	return 0;
}

static void __exit hello_param_exit(void)
{
	printk("GoodBye, world \n");
}

module_init(hello_param_init);
module_exit(hello_param_exit);


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment