系统调用

#define SYSCALL_DEFINE0(name)	   asmlinkage long sys_##name(void)
#define SYSCALL_DEFINE1(name, ...) SYSCALL_DEFINEx(1, _##name, __VA_ARGS__)
#define SYSCALL_DEFINE2(name, ...) SYSCALL_DEFINEx(2, _##name, __VA_ARGS__)
#define SYSCALL_DEFINE3(name, ...) SYSCALL_DEFINEx(3, _##name, __VA_ARGS__)
#define SYSCALL_DEFINE4(name, ...) SYSCALL_DEFINEx(4, _##name, __VA_ARGS__)
#define SYSCALL_DEFINE5(name, ...) SYSCALL_DEFINEx(5, _##name, __VA_ARGS__)
#define SYSCALL_DEFINE6(name, ...) SYSCALL_DEFINEx(6, _##name, __VA_ARGS__)
--->
	#define SYSCALL_DEFINEx(x, sname, ...) __SYSCALL_DEFINEx(x, sname, __VA_ARGS__)
	--->
		asmlinkage long sys##name(__SC_DECL##x(__VA_ARGS__))


(1)、open系统调用
open(); --->
	/*/fs/open.c*/
	SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode);--->
		do_sys_open(); --->
			fd = get_unused_fd_flags();//分配FD
			do_filp_open(); --->
	/*普通文件*/-->	__open_namei_create();--->	//这里打开文件
						vfs_create(); --->
							dir->i_op->create(dir, dentry, mode, nd);
					
	/*驱动文件*/-->	filp = nameidata_to_filp(&nd, open_flag);--->//这里打开驱动文件
						__dentry_open(); --->
							//如果没有提供open回调函数,并且驱动中有open接口
							//那么就调用驱动的open()函数
							open = f->f_op->open;
	
	

(2)、write系统调用
write(); --->
	//fs/read_write.c
	SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,size_t, count);--->
		vfs_write(file, buf, count, &pos);--->
			if (file->f_op->write)
				ret = file->f_op->write(file, buf, count, pos); //调用实现的write函数
			else
				do_sync_write();--->
					ret = filp->f_op->aio_write(&kiocb, &iov, 1, kiocb.ki_pos);//异步写
					
(3)、read系统调用
read(); --->
	SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count); --->
		vfs_read(); --->
			if (file->f_op->read)
				ret = file->f_op->read(file, buf, count, pos);//这里调用read函数
			else
				ret = do_sync_read(file, buf, count, pos);//异步读
			

(4)、poll系统调用
poll() --->
	//fs/select.c
	SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,long, timeout_msecs);--->
		do_sys_poll(); --->
			do_poll(); --->
				do_pollfd(); --->
					file->f_op->poll(file, pwait); //驱动程序里实现的poll函数
				poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack);//真正进入睡眠
				
				
(5)、select系统调用
select(); --->
	SYSCALL_DEFINE5(select,..........);--->
		core_sys_select(); --->
			do_select(); --->
				poll_initwait(&table);
				for(;;){
					mask = (*f_op->poll)(file, wait); //调用poll函数
					....
					poll_schedule_timeout();//真正进入睡眠
				}
				poll_freewait(&table);
				
			
(6)、ioctl系统调用
ioctl(); --->
	//fs/ioctl.c
	SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg);--->
		do_vfs_ioctl();--->
			vfs_ioctl(filp, cmd, arg); --->
				if (filp->f_op->unlocked_ioctl) 
					filp->f_op->unlocked_ioctl(filp, cmd, arg);
				else if (filp->f_op->ioctl)
					filp->f_op->ioctl(filp->f_path.dentry->d_inode,filp, cmd, arg);
					
				
(7)、mmap系统调用
mmap(); --->
	//arch/powerpc/kernel/syscall.c
	sys_mmap(); --->
		do_mmap2();--->
			do_mmap_pgoff();--->
				mmap_region();--->
					file->f_op->mmap(file, vma);
						
		

(6)、close系统调用
close(); --->
	//fs/open.c
	SYSCALL_DEFINE1(close, unsigned int, fd);--->
		filp_close(filp, files);--->
			if (filp->f_op && filp->f_op->flush)
				filp->f_op->flush(filp, id);
			

你可能感兴趣的:(系统调用)