13
module_init(hello_init); module_exit(hello_exit); 모모 모모모 LE_LICENSE(“Dual BSD/GPL”) LE_AUTHOR(“UNIX CLASS”) LE_DESCRIPTION(“TEST MODULE”) le_param short, ushort, int, uint, long, ulong, charp, bool, intarray(int

module_init(hello_init); module_exit(hello_exit);

Embed Size (px)

DESCRIPTION

모듈 초기화. module_init(hello_init); module_exit(hello_exit);. MODULE_LICENSE(“Dual BSD/GPL”) MODULE_AUTHOR(“UNIX CLASS”) MODULE_DESCRIPTION(“TEST MODULE”) module_param short, ushort, int, uint, long, ulong, charp, bool, intarray(int *). #include #include - PowerPoint PPT Presentation

Citation preview

Page 1: module_init(hello_init); module_exit(hello_exit);

module_init(hello_init);module_exit(hello_exit);

모듈 초기화

MODULE_LICENSE(“Dual BSD/GPL”)MODULE_AUTHOR(“UNIX CLASS”)MODULE_DESCRIPTION(“TEST MODULE”)module_param

short, ushort, int, uint, long, ulong, charp, bool, intarray(int *)

Page 2: module_init(hello_init); module_exit(hello_exit);

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

static int onevalue = 1;static char *twostring = NULL;

module_param(onevalue, int, 0);module_param(twostring, charp, 0);

static int hello_init(void){ printk("Hello, world [onevalue=%d:twostring=%s]\n", onevalue, twostring ); return 0;}

static void hello_exit(void){ printk("Goodbye, world\n");}

module_init(hello_init);module_exit(hello_exit);

MODULE_AUTHOR(“”);MODULE_DESCRIPTION(“Module Parameter Test Module”);MODULE_LICENSE(“Dual BSD/GPL”);

Page 3: module_init(hello_init); module_exit(hello_exit);
Page 4: module_init(hello_init); module_exit(hello_exit);
Page 5: module_init(hello_init); module_exit(hello_exit);

이식성과 데이터형

App Kernel

App Kernel

__s8 s8 부호있는 8 비트 정수형 __u8 u8 부호없는

8 비트 정수형

__s16 s16 부호있는 16 비트 정수형 __u1

6u16 부호없는

16 비트 정수형

__s32 s32 부호있는 32 비트 정수형 __u3

2u32 부호없는

32 비트 정수형

__s64 s64 부호있는 64 비트 정수형 __u6

4u64 부호없는

64 비트 정수형

- 서로 다른 프로세서 상에서의 이식성을 위해 가급적 리눅스 커널이 제공하는 데이터형을 사용하는 것이 좋다 .- #include <asm/types.h> 에 정의 .- 주로 사용되는 데이터형

Page 6: module_init(hello_init); module_exit(hello_exit);

동적 메모리 할당 함수

kmalloc()kfree()

할당 속도가 빠르고 사용법이 간단해 디바이스 드라이버에서 가장 많이 사용되는 함수 ( 할당 크기 제한 있음 ).

vmalloc()vfree()

가상공간이 허용하는 한 크기 제한 없이할당 받을 수 있다 . 큰 메모리 공간을할당할 때 주로 사용한다 ( 속도가 느리다 ).

__get_free_pages()__free_pages() 잘 사용되지 않는 함수 .

Page 7: module_init(hello_init); module_exit(hello_exit);

kmalloc(), kfree()-1메모리주소 kmalloc( 할당 받을 크기 , 옵션 );kfree( 메모리주소 );

* 할당 가능한 최대 크기는 32 x PAGE_SIZE ( 일반적으로 128KB)

#include <linux/slab.h>char *buf;buf = kmalloc(1024, GFP_KERNEL);if (buf != NULL){ … kfree(buf);}

Page 8: module_init(hello_init); module_exit(hello_exit);

kmalloc(), kfree()-2

GFP_KERNEL동적 메모리 할당이 항상 성공하도록 요구한다 . 메모리 부족 시 메모리가 사용 가능해 질 때까지 잠든다 . 이런 특성으로 인해서 인터럽트에서는 사용하면 안 된다 .

GFP_ATOMIC커널에 할당 가능한 메모리가 있으면 무조건 할당하고 , 없으면 즉시 NULL 을 반환한다 .프로세스가 잠드는 문제는 없지만 메모리 할당에 실패할 경우를 항상 염두 해 두어야 한다 .

GFP_DMA 연속된 물리 메모리를 할당 받을 때 사용한다 .(DMA 를 사용할 때 사용해야 한다 .)

메모리 할당 시 사용되는 옵션( 할당된 메모리에 특성을 주거나 할당 시점에 처리방식을 지정 )

Page 9: module_init(hello_init); module_exit(hello_exit);

vmalloc(), vfree()메모리주소 vmalloc( 할당 받을 크기 );vfree( 메모리주소 );#include <linux/vmalloc.h>char *buf;buf = vmalloc(1024);if (buf != NULL){ … vfree(buf);}

Page 10: module_init(hello_init); module_exit(hello_exit);

vmalloc(), vfree() 특징

- 가상 주소 공간에서 할당받기 때문에 해당주소의 영역이 하드디스크에 있을 수도 있어 실패할 수 있다 .

- 커다란 연속된 공간을 할당하기 위해 가상메모리 관리 루틴이 수행되기 때문에 kmalloc() 보다 속도가 매우 느리다 .

- 할당 시 프로세스가 잠들지 못하게 할 수 없기 때문에 인터럽트 서비스 함수 안에서는 사용할 수 없다 .

Page 11: module_init(hello_init); module_exit(hello_exit);

__get_free_pages(), free_pages()

메모리주소 __get_free_pages( 옵션 , 차수 );free_pages( 메모리주소 );#include <linux/mm.h> //2.4#include <linux/gfp.h> //2.6#include <asm/page.h> //get_orderchar *buf;

buf = __get_free_pages(GFP_KERNEL, order); /* order=0 이면 1 개의 page, order=3 이면 3 개의 page /* MAX_ORDER 은 11, 그러나 5 이하의 값만 사용하는 것이 안전 (32*PAGE_SIZE) */if (buf != NULL){ … free_pages(buf, order);}

Page 12: module_init(hello_init); module_exit(hello_exit);
Page 13: module_init(hello_init); module_exit(hello_exit);