Linux driver Development What to know (iii)------CONTAINER_OF, timers and system calls

Source: Internet
Author: User

CONTAINER_OF Macro

Defined:

/**

* Container_of-cast a member of a structure out to the containing structure

* @ptr: The pointer to the member.

* @type: The type of the container struct this was embedded in.

* @member: The name of the member within the struct.

*

*/

#define CONTAINER_OF (PTR, type, member) ({\

Const typeof (((type *) 0)->member) * __mptr = (PTR); \

(Type *) ((char *) __mptr-offsetof (type, member)); })

#endif

The first step:

Const typeof ((type *) 0->member) * __mptr= (PTR)

Struct list_head *__mptr=(ptr)

Step Two:

(Type *) (char *) __mptr-offsetof (Type,menber)

0x***-offset

Step Three:offsetof (Type,member)

((TYPE *) 0)->member

& (TYPE *) 0)->menber

Kernel timers

Clock interrupt

HZ: constant determines the frequency at which the clock occurs

Tick: Time interval at which time breaks occur tick=1/hz

Jiffies: Core variables,

struct timer_list

{

unsigned long expires; Timeout period

void (*function) (unsigned long0) processing function

unsigned long data; The parameters passed to it when the kernel call time-out handler function

}

Static initialization

Timer_initalizer (_function,_expires,_data)

Define_time (_name,_function,_expires,_data);

_name The name of the kernel timer variable you want to define

Void init_timer (struct timer_list *timer);

Modify Timer

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/8A/BE/wKiom1g6MEviN7l2AACB4QttqMw102.png-wh_500x0-wm_3 -wmp_4-s_1419104650.png "title=" image 1.png "alt=" Wkiom1g6mevin7l2aacb4qttqmw102.png-wh_50 "/>

Init_timer (time) initialize timer, pass address

#define Bark_timer 5

#deifne Bark_data 100

Xxx.expires = jiffies+hz*bark_timer;//bark_timer=5 Five seconds

Xxx.function=xxxxxx

Xxxx.data=bark_data;

Add_timer (&xxxxx

Del_timer ()

2 system calls

ANSI c fopen( library function for standard C)

UNIX C Open (System call)

User-state programs generally do not have access to kernel-state resources, only through interrupts or system calls to enter the kernel state from the user state

2.6.35 has a 366 Kernel system call for application services.

The principle of system invocation:

The application first populates the register with the appropriate values and then calls a special instruction to jump to a fixed location in the kernel, which finds the appropriate function execution based on the fixed values populated by the application.

appropriate value:arch/arm/asm/unistd.h

Call Special instruction Arm->swi(soft interrupt)

X86 0x80

fixed position: In the arm system, the application jumps to Entry-conmmon->vector_swi and uses the sys_call_table (Calls. S)

User space Programming fd=open("aa.txt"... )

5-> Register

Swi

____________________

Vector_swi

SYS_CALL_TABLE[5]

Sys_open

SYSCALL_DEFINR3 Stitching

1) Add a new kernel function

Vim ARCH/ARM/KERNEL/SYS_ARM.C

asmlinkage int sys_add (int x,int y)

{

PRINTK ("entry sys_add!\n");

Return X+y;

}

2) Update header file arch/arm/include/asm/unistd.h

#define __NR_ADD (__nr_syscall_base+366)

3) update the system call table (calls. S)

Call (Sys_add)

The final make can be


This article is from the "Mao Scattered People" blog, please be sure to keep this source http://songmao.blog.51cto.com/11700139/1876960

Linux driver Development What to know (iii)------CONTAINER_OF, timers and system calls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.