Notes
main
main
  • Introduction
  • linuxKernel
    • tips
    • make_help
    • old linux
      • compile_linux0.11
      • TestEnvironment
      • load_setup
      • get_hard_data
    • list
    • plist
    • fifo
    • idr
    • xarray
    • rbtree
    • maple_tree
    • syscall
    • bitmap
    • page
    • page_flags
    • page_size
    • page mapcount
    • page refcount
    • folio
    • slub
      • proc_slabinfo
      • slub_theory
      • kmalloc_kfree
      • kmem_cache
      • slab_alloc
      • slab_free
      • proc_meminfo_SReclaimable_SReclaimable
    • vmalloc
    • brk
    • mmap
    • mremap
    • mprotect
    • madvise
    • read
    • write
    • shmem
    • huge_page
    • page_fault
    • rmap
    • lru
    • multi-gen-LRU
    • page_reclaim
    • page_cache
    • page_table
    • rcu
    • kvm
    • aarch64_boot
    • tracing_system
    • cache_coherence_and_memory_consistency
    • cpu_speculates
    • mmap_lock
    • per-vma_lock
    • cgroup
    • symbol
    • campact
    • page_ext
    • mempool
    • kernelstack
    • filesystem
    • io_stack
    • workingset
    • ioremap
    • sched_period
  • linuxDebug
    • openocd_openjtag
    • i2c_tools
    • objdump
    • addr2line
    • gdb_useage
    • debug_linux_kernel_via_gdb
    • debug_linux_module_via_gdb
    • early_boot
    • sequentially_execute
    • dynamic_debug
    • research_linuxKernel_by_patch
    • tracefs
    • ebpf
    • bpftrace
    • perf
    • flame_graph
    • crash
    • ASAN_HWASAN_MTE_check_mem_bug
    • page_owner
    • vmtouch
    • fio
    • benchmark
  • linuxSystem
    • common
      • system_version
      • procfs
      • proc_sys_vm
      • cmd_ps
      • makefile
      • file_descriptor
      • psi
      • ulimit
      • top
      • delay_accounting
    • ubuntu
      • custom_kernel
      • get_cmd_src
      • record_ssh_info
      • log
      • run_custom_script
      • repo
      • cockpit
      • nfs
      • tftp
      • misc
    • fedora
      • system_upgrade
      • custom_kernel
      • lvextend
      • yt-dlp
      • jellyfin
  • linuxDriver
    • i2c_peripherals_driver
    • spi_peripherals_driver
    • gpio_subsystem
    • IRQ_driver
    • blockIO_unblockIO_async
    • linux_own_driver
    • misc_device
    • input_device
    • timer
    • atomic_spinlock_semaphore_mutex
    • lcd
    • touch_screen
    • debugfs
    • v4l2
    • mmap
  • hardware
    • paging_mmu_pt
    • iommu
  • process_thread_scheduler
    • scheduler01
    • scheduler02
    • scheduler03
    • scheduler04
    • scheduler05
    • scheduler06
  • memory_management
    • mm1
    • mm2
    • mm3
    • mm4
    • mm5
  • input_output_filesystem
    • io_fs_01
    • io_fs_02
    • io_fs_03
    • io_fs_04
  • lock_and_lockup_detector
    • general_lock
    • hung_task
    • softLockup_hardLockup
    • crash_experiment
  • MIT_6.S081
    • 6.S081_Operating_System_Engineering
    • Schedule.md
    • Class
      • Overview
      • Administrivia
    • Labs
      • Tools
      • Guidance
      • startup
      • syscall
      • page_table
      • Calling_Convention
      • traps
    • xv6
      • xv6
    • References.md
  • qemu
    • qemu_buildroot
    • qemu_busybox.md
    • Serial.md
    • demo_mini2440
      • 0_compilation_error_summary
      • 1_compilation_steps
      • 2_operation_mode
      • 3_transplant_tools_libraries
      • 4_tools_use
      • reference_website
  • tools
    • getKernelSourceCodeList
    • nat
    • shell
    • translating
    • YouCompleteMe
    • cscope
    • global
    • vscode
    • vim
    • binary
    • markdown
    • draw
    • git
    • tig
    • tmux
    • mail_client
    • download_patchset_from_LKML
    • minicom
    • clash
  • other
    • interview
    • interview_c_base
    • know_dontknow
    • Stop-Ask-Questions-The-Stupid-Ways
    • How-To-Ask-Questions-The-Smart-Way
    • docker
    • buildroot
    • rv32_to_rv64
Powered by GitBook
On this page
  • base linux-2.6.34
  • 零参数系统调用
  • base linux-5.4
  • 零参数系统调用
  • 整型参数系统调用
  • 字符串参数系统调用
  • 数组参数系统调用
  • 指针参数系统调用

Was this helpful?

  1. linuxKernel

syscall

系统调用(system call)是用户空间(user space)与内核空间(kernel space)通信的桥梁,其中应用层 glibc/uclibc库 等的实现,就是对系统调用进行封装,所以想从应用层向内核层深入研究,必须先对系统调用有一定了解。

base linux-2.6.34

零参数系统调用

  1. 内核层添加系统调用

  • 系统调用入口

    $ vim arch/x86/include/asm/unistd_64.h
    #define __NR_foo				300
    __SYSCALL(__NR_foo, sys_foo)
  • 系统调用实现

    $ vim kernel/sys.c
    #include <linux/syscalls.h>
    
    SYSCALL_DEFINE0(foo)
    {
    	printk("%s: syscall test\n", __func__);
    	return 0;
    }
  1. 应用层调用系统调用

    $ vim syscall.c
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/syscall.h>
    
    #define SYSCALL_FOO		300
    
    void syscall_0(void)
    {
    	syscall(SYSCALL_FOO);
    }

base linux-5.4

零参数系统调用

  1. 内核层添加系统调用

  • 系统调用入口

    $ vim arch/x86/entry/syscalls/syscall_64.tbl
    436	common	syscall_0			__x64_sys_syscall_0
  • 系统调用实现

    $ vim fs/syscall.c
    #include <linux/syscalls.h>
    
    SYSCALL_DEFINE0(syscall_0)
    {
    	printk("%s\n", __func__);
    	return 0;
    }
  1. 应用层调用系统调用

    $ vim syscall.c
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/syscall.h>
    
    #define SYSCALL_0		436
    
    void syscall_0(void)
    {
    	syscall(SYSCALL_0);
    }

整型参数系统调用

  1. 内核层添加系统调用

  • 系统调用入口

    $ vim arch/x86/entry/syscalls/syscall_64.tbl
    437	common	syscall_int1		__x64_sys_syscall_int1
    438	common	syscall_int2		__x64_sys_syscall_int2
  • 系统调用实现

    $ vim fs/syscall.c
    #include <linux/syscalls.h>
    
    SYSCALL_DEFINE1(syscall_int1, int, parameter1)
    {
    	printk("%s: %d\n", __func__, parameter1);
    	return 0;
    }
    
    SYSCALL_DEFINE2(syscall_int2, int, parameter1, int, parameter2)
    {
    	printk("%s: %d, %d\n", __func__, parameter1, parameter2);
    	return 0;
    }
  1. 应用层调用系统调用

    $ vim syscall.c
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/syscall.h>
    
    #define SYSCALL_INT1	437
    #define SYSCALL_INT2	438
    
    void syscall_int1(void)
    {
    	int p1 = 0x01;
    
    	syscall(SYSCALL_INT1, p1);
    }
    
    void syscall_int2(void)
    {
    	int p1 = 0x01;
    	int p2 = 0x02;
    
    	syscall(SYSCALL_INT2, p1, p2);
    }

字符串参数系统调用

  1. 内核层添加系统调用

  • 系统调用入口

    $ vim arch/x86/entry/syscalls/syscall_64.tbl
    439	common	syscall_str1		__x64_sys_syscall_str1
  • 系统调用实现

    $ vim fs/syscall.c
    #include <linux/syscalls.h>
    #include <linux/string.h>
    
    SYSCALL_DEFINE1(syscall_str1, char __user *, parameter1)
    {
    	char kbuf[64];
    	unsigned long ret;
    
    	ret = copy_from_user(kbuf, parameter1, strlen(parameter1)+1);
    	printk("%s: user to kernel: %s\n", __func__, kbuf);
    
    	strcpy(kbuf, "kernel");
    	ret = copy_to_user(parameter1, kbuf, strlen(kbuf)+1);
    
    	return 0;
    }
  1. 应用层调用系统调用

    $ vim syscall.c
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/syscall.h>
    
    #define SYSCALL_STR1	439
    
    void syscall_str1(void)
    {
    	char p1[64] = "hello world";
    
    	syscall(SYSCALL_STR1, p1);
    	printf("kernel to user: %s\n", p1);
    }

数组参数系统调用

  1. 内核层添加系统调用

  • 系统调用入口

    $ vim arch/x86/entry/syscalls/syscall_64.tbl
    440	common	syscall_array1		__x64_sys_syscall_array1
  • 系统调用实现

    $ vim fs/syscall.c
    #include <linux/syscalls.h>
    
    SYSCALL_DEFINE1(syscall_array1, int __user *, parameter1)
    {
    	int karray[3];
    	unsigned long ret;
    
    	ret = copy_from_user(karray, parameter1, sizeof(int) * 3);
    	printk("%s: %d %d %d\n",
    		__func__, karray[0], karray[1], karray[2]);
    
    	return 0;
    }
  1. 应用层调用系统调用

    $ vim syscall.c
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/syscall.h>
    
    #define SYSCALL_ARRAY1	440
    
    void syscall_array1(void)
    {
    	int p1[3] = {3, 4, 5};
    
    	syscall(SYSCALL_ARRAY1, p1);
    }

指针参数系统调用

  1. 内核层添加系统调用

  • 系统调用入口

    $ vim arch/x86/entry/syscalls/syscall_64.tbl
    441	common	syscall_pointer1	__x64_sys_syscall_pointer1
  • 系统调用实现

    $ vim fs/syscall.c
    #include <linux/syscalls.h>
    
    SYSCALL_DEFINE1(syscall_pointer1, int __user *, parameter1)
    {
    	int ktest;
    	unsigned long ret;
    
    	ret = copy_from_user(&ktest, parameter1, sizeof(int));
    	printk("%s: %d\n", __func__, ktest);
    
    	return 0;
    }
  1. 应用层调用系统调用

    $ vim syscall.c
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/syscall.h>
    
    #define SYSCALL_POINTER1	441
    
    void syscall_pointer1(void)
    {
    	int p1 = 6;
    
    	syscall(SYSCALL_POINTER1, &p1);
    }
Previousmaple_treeNextbitmap

Last updated 3 years ago

Was this helpful?