2015年10月25日 星期日

add new system call for arm linux

網路上有很多增加一個 system call for arm linux 的文章, 不過都有些問題, 不是過時了就是那裡怪怪的。

我用 raspberry pi 2 作為平台, linx 4.1.y 為練習的版本, 還蠻新的, 應該可以撐好幾年。

總共要改這些:
modified: arch/arm/include/asm/unistd.h
modified: arch/arm/include/uapi/asm/unistd.h
modified: include/linux/syscalls.h
modified: arch/arm/kernel/Makefile
modified: arch/arm/kernel/calls.S
new file: arch/arm/kernel/my_syscall.c

參考 add_arm_linux_new_syscall.patch, 值得注意的是 L10 從 388 改成 392, 奇怪, 不是才加入一個 system call 到 calls.S 嗎? 怎麼不是 389?? 大栽問?

add_arm_linux_new_syscall.patch
  1 diff --git a/arch/arm/include/asm/unistd.h b/arch/arm/include/asm/unistd.h
  2 index 32640c4..7cba573 100644
  3 --- a/arch/arm/include/asm/unistd.h
  4 +++ b/arch/arm/include/asm/unistd.h
  5 @@ -19,7 +19,7 @@
  6   * This may need to be greater than __NR_last_syscall+1 in order to
  7   * account for the padding in the syscall table
  8   */
  9 -#define __NR_syscalls  (388)
 10 +#define __NR_syscalls  (392)
 11  
 12  /*
 13   * *NOTE*: This is a ghost syscall private to the kernel.  Only the
 14 diff --git a/arch/arm/include/uapi/asm/unistd.h b/arch/arm/include/uapi/asm/unistd.h
 15 index 0c3f5a0..20031a2 100644
 16 --- a/arch/arm/include/uapi/asm/unistd.h
 17 +++ b/arch/arm/include/uapi/asm/unistd.h
 18 @@ -414,6 +414,7 @@
 19  #define __NR_memfd_create  (__NR_SYSCALL_BASE+385)
 20  #define __NR_bpf   (__NR_SYSCALL_BASE+386)
 21  #define __NR_execveat   (__NR_SYSCALL_BASE+387)
 22 +#define __NR_get_mmu_reg  (__NR_SYSCALL_BASE+388)
 23  
 24  /*
 25   * The following SWIs are ARM private.
 26 diff --git a/arch/arm/kernel/Makefile b/arch/arm/kernel/Makefile
 27 index 752725d..c6fc56d 100644
 28 --- a/arch/arm/kernel/Makefile
 29 +++ b/arch/arm/kernel/Makefile
 30 @@ -18,7 +18,7 @@ CFLAGS_REMOVE_return_address.o = -pg
 31  obj-y  := elf.o entry-common.o irq.o opcodes.o \
 32       process.o ptrace.o reboot.o return_address.o \
 33       setup.o signal.o sigreturn_codes.o \
 34 -     stacktrace.o sys_arm.o time.o traps.o
 35 +     stacktrace.o sys_arm.o time.o traps.o my_syscall.o
 36  
 37  obj-$(CONFIG_ATAGS)  += atags_parse.o
 38  obj-$(CONFIG_ATAGS_PROC) += atags_proc.o
 39 diff --git a/arch/arm/kernel/calls.S b/arch/arm/kernel/calls.S
 40 index 05745eb..a715f70 100644
 41 --- a/arch/arm/kernel/calls.S
 42 +++ b/arch/arm/kernel/calls.S
 43 @@ -397,6 +397,7 @@
 44  /* 385 */ CALL(sys_memfd_create)
 45    CALL(sys_bpf)
 46    CALL(sys_execveat)
 47 +  CALL(sys_get_mmu_reg)
 48  #ifndef syscalls_counted
 49  .equ syscalls_padding, ((NR_syscalls + 3) & ~3) - NR_syscalls
 50  #define syscalls_counted
 51 diff --git a/arch/arm/kernel/my_syscall.c b/arch/arm/kernel/my_syscall.c
 52 new file mode 100644
 53 index 0000000..d61144f
 54 --- /dev/null
 55 +++ b/arch/arm/kernel/my_syscall.c
 56 @@ -0,0 +1,87 @@
 57 +#include <linux/linkage.h>
 58 +#include <linux/kernel.h>
 59 +#include <linux/syscalls.h>
 60 +
 61 +
 62 +typedef unsigned int u32;
 63 +
 64 +static void get_ttbr1(u32 *ttbr1)
 65 +{
 66 +  asm
 67 +  (
 68 +    "mrc p15,0,%0,c2,c0,1\n"
 69 +    :"=r"(*ttbr1)
 70 +    :
 71 +    :
 72 +  );
 73 +/*
 74 + * ref: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0211h/Bhchbicd.html
 75 +MRC p15, 0, <Rd>, c2, c0, 1            ; Read Translation Table Base Register 1
 76 +MCR p15, 0, <Rd>, c2, c0, 1            ; Write Translation Table Base Register 1 
 77 +*/
 78 +}
 79 +
 80 +static void get_ttbr0(u32 *ttbr0)
 81 +{
 82 +  u32 a;
 83 +  asm
 84 +  (
 85 +    "mrc p15,0,%0,c2,c0,0\n"
 86 +    :"=r"(a)
 87 +    :
 88 +    :
 89 +  );
 90 +  *ttbr0 = a;
 91 +  printk(KERN_ALERT "a: %x\n", a);
 92 +}
 93 +
 94 +
 95 +static void get_ttbcr(u32 *reg)
 96 +{
 97 +  asm
 98 +  (
 99 +    "mrc p15,0,%0,c2,c0,2\n"
100 +    :"=r"(*reg)
101 +    :
102 +    :
103 +  );
104 +
105 +#if 0
106 +http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0211k/Bihgfcgf.html
107 +MRC p15, 0, <Rd>, c2, c0, 2      ; Read Translation Table Base Control Register
108 +MCR p15, 0, <Rd>, c2, c0, 2      ; Write Translation Table Base Control Register
109 +#endif
110 +}
111 +
112 +
113 +SYSCALL_DEFINE1(get_mmu_reg, unsigned int, reg_type)
114 +{
115 +  int reg = 0x12345678;
116 +  printk(KERN_ALERT "reg_type: %x\n", reg_type);
117 +  switch (reg_type)
118 +  {
119 +    case 0:
120 +    {
121 +      get_ttbr0(&reg);
122 +      break;
123 +    }
124 +    case 1:
125 +    {
126 +      get_ttbr1(&reg);
127 +      break;
128 +    }
129 +    case 2:
130 +    {
131 +      get_ttbcr(&reg);
132 +      break;
133 +    }
134 +    default:
135 +    {
136 +      printk(KERN_ALERT "not support reg_type: %x\n", reg_type);
137 +      return -5;
138 +      break;
139 +    }
140 +  }
141 +  return reg;
142 +}
143 +
144 diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h
145 index 76d1e38..f1aac22 100644
146 --- a/include/linux/syscalls.h
147 +++ b/include/linux/syscalls.h
148 @@ -701,6 +701,7 @@ asmlinkage long sys_sysfs(int option,
149  asmlinkage long sys_syslog(int type, char __user *buf, int len);
150  asmlinkage long sys_uselib(const char __user *library);
151  asmlinkage long sys_ni_syscall(void);
152 +asmlinkage long sys_get_mmu_reg(unsigned int reg_type);
153  asmlinkage long sys_ptrace(long request, long pid, unsigned long addr,
154        unsigned long data);
155  
156 @@ -825,6 +826,7 @@ asmlinkage long sys_fanotify_mark(int fanotify_fd, unsigned int flags,
157  asmlinkage long sys_syncfs(int fd);
158  
159  asmlinkage long sys_fork(void);
160 +asmlinkage long sys_get_mmu_reg(unsigned int reg_type);
161  asmlinkage long sys_vfork(void);
162  #ifdef CONFIG_CLONE_BACKWARDS
163  asmlinkage long sys_clone(unsigned long, unsigned long, int __user *, int,

如果單純的 +1, compile 很有可能會看到:

arch/arm/kernel/entry-common.S:110: Error: __NR_syscalls is not equal to the size of the syscall table

原因是 arch/arm/kernel/entry-common.S, arch/arm/kernel/calls.S 在計算 a.c L1043~L1045, 沒符合程式邏輯罷了, 解決方法很簡單, 拿到這段檢查程式就好了, 優雅的方法就是去計算其規則, 讓結果符合這個檢查, 392 就是我算出的結果。

cpp.sh L1 是我將 arch/arm/kernel/entry-common.S 展開, L2 是單純的用 as compile, 照著規則算出來看多少才會過關。

這段很難說明, 所以就不說了, a.c L1029 是關鍵, 補上需要的數量, 真的搞不定就拿掉吧!

a.c
 634 
 635  .equ NR_syscalls,0
 636 
 637 # 1 "arch/arm/kernel/calls.S" 1
 638 # 12 "arch/arm/kernel/calls.S"
 639          .equ NR_syscalls,NR_syscalls+1
 640   .equ NR_syscalls,NR_syscalls+1
 641   .equ NR_syscalls,NR_syscalls+1
 642   .equ NR_syscalls,NR_syscalls+1
 643   .equ NR_syscalls,NR_syscalls+1
 644          .equ NR_syscalls,NR_syscalls+1
 645   .equ NR_syscalls,NR_syscalls+1
 646   .equ NR_syscalls,NR_syscalls+1
 647   .equ NR_syscalls,NR_syscalls+1
 648   .equ NR_syscalls,NR_syscalls+1
 649          .equ NR_syscalls,NR_syscalls+1
 650   .equ NR_syscalls,NR_syscalls+1
 651   .equ NR_syscalls,NR_syscalls+1
 652   .equ NR_syscalls,NR_syscalls+1
 653   .equ NR_syscalls,NR_syscalls+1
 654          .equ NR_syscalls,NR_syscalls+1
 655   .equ NR_syscalls,NR_syscalls+1
 656   .equ NR_syscalls,NR_syscalls+1
 657   .equ NR_syscalls,NR_syscalls+1
 658   .equ NR_syscalls,NR_syscalls+1
 659          .equ NR_syscalls,NR_syscalls+1
 660   .equ NR_syscalls,NR_syscalls+1
 661   .equ NR_syscalls,NR_syscalls+1
 662   .equ NR_syscalls,NR_syscalls+1
 663   .equ NR_syscalls,NR_syscalls+1
 664          .equ NR_syscalls,NR_syscalls+1
 665   .equ NR_syscalls,NR_syscalls+1
 666   .equ NR_syscalls,NR_syscalls+1
 667   .equ NR_syscalls,NR_syscalls+1
 668   .equ NR_syscalls,NR_syscalls+1
 669          .equ NR_syscalls,NR_syscalls+1
 670   .equ NR_syscalls,NR_syscalls+1
 671   .equ NR_syscalls,NR_syscalls+1
 672   .equ NR_syscalls,NR_syscalls+1
 673   .equ NR_syscalls,NR_syscalls+1
 674          .equ NR_syscalls,NR_syscalls+1
 675   .equ NR_syscalls,NR_syscalls+1
 676   .equ NR_syscalls,NR_syscalls+1
 677   .equ NR_syscalls,NR_syscalls+1
 678   .equ NR_syscalls,NR_syscalls+1
 679          .equ NR_syscalls,NR_syscalls+1
 680   .equ NR_syscalls,NR_syscalls+1
 681   .equ NR_syscalls,NR_syscalls+1
 682   .equ NR_syscalls,NR_syscalls+1
 683   .equ NR_syscalls,NR_syscalls+1
 684          .equ NR_syscalls,NR_syscalls+1
 685   .equ NR_syscalls,NR_syscalls+1
 686   .equ NR_syscalls,NR_syscalls+1
 687   .equ NR_syscalls,NR_syscalls+1
 688   .equ NR_syscalls,NR_syscalls+1
 689          .equ NR_syscalls,NR_syscalls+1
 690   .equ NR_syscalls,NR_syscalls+1
 691   .equ NR_syscalls,NR_syscalls+1
 692   .equ NR_syscalls,NR_syscalls+1
 693   .equ NR_syscalls,NR_syscalls+1
 694          .equ NR_syscalls,NR_syscalls+1
 695   .equ NR_syscalls,NR_syscalls+1
 696   .equ NR_syscalls,NR_syscalls+1
 697   .equ NR_syscalls,NR_syscalls+1
 698   .equ NR_syscalls,NR_syscalls+1
 699          .equ NR_syscalls,NR_syscalls+1
 700   .equ NR_syscalls,NR_syscalls+1
 701   .equ NR_syscalls,NR_syscalls+1
 702   .equ NR_syscalls,NR_syscalls+1
 703   .equ NR_syscalls,NR_syscalls+1
 704          .equ NR_syscalls,NR_syscalls+1
 705   .equ NR_syscalls,NR_syscalls+1
 706   .equ NR_syscalls,NR_syscalls+1
 707   .equ NR_syscalls,NR_syscalls+1
 708   .equ NR_syscalls,NR_syscalls+1
 709          .equ NR_syscalls,NR_syscalls+1
 710   .equ NR_syscalls,NR_syscalls+1
 711   .equ NR_syscalls,NR_syscalls+1
 712   .equ NR_syscalls,NR_syscalls+1
 713   .equ NR_syscalls,NR_syscalls+1
 714          .equ NR_syscalls,NR_syscalls+1
 715   .equ NR_syscalls,NR_syscalls+1
 716   .equ NR_syscalls,NR_syscalls+1
 717   .equ NR_syscalls,NR_syscalls+1
 718   .equ NR_syscalls,NR_syscalls+1
 719          .equ NR_syscalls,NR_syscalls+1
 720   .equ NR_syscalls,NR_syscalls+1
 721   .equ NR_syscalls,NR_syscalls+1
 722   .equ NR_syscalls,NR_syscalls+1
 723   .equ NR_syscalls,NR_syscalls+1
 724          .equ NR_syscalls,NR_syscalls+1
 725   .equ NR_syscalls,NR_syscalls+1
 726   .equ NR_syscalls,NR_syscalls+1
 727   .equ NR_syscalls,NR_syscalls+1
 728   .equ NR_syscalls,NR_syscalls+1
 729          .equ NR_syscalls,NR_syscalls+1
 730   .equ NR_syscalls,NR_syscalls+1
 731   .equ NR_syscalls,NR_syscalls+1
 732   .equ NR_syscalls,NR_syscalls+1
 733   .equ NR_syscalls,NR_syscalls+1
 734          .equ NR_syscalls,NR_syscalls+1
 735   .equ NR_syscalls,NR_syscalls+1
 736   .equ NR_syscalls,NR_syscalls+1
 737   .equ NR_syscalls,NR_syscalls+1
 738   .equ NR_syscalls,NR_syscalls+1
 739           .equ NR_syscalls,NR_syscalls+1
 740   .equ NR_syscalls,NR_syscalls+1
 741   .equ NR_syscalls,NR_syscalls+1
 742   .equ NR_syscalls,NR_syscalls+1
 743   .equ NR_syscalls,NR_syscalls+1
 744           .equ NR_syscalls,NR_syscalls+1
 745   .equ NR_syscalls,NR_syscalls+1
 746   .equ NR_syscalls,NR_syscalls+1
 747   .equ NR_syscalls,NR_syscalls+1
 748   .equ NR_syscalls,NR_syscalls+1
 749           .equ NR_syscalls,NR_syscalls+1
 750   .equ NR_syscalls,NR_syscalls+1
 751   .equ NR_syscalls,NR_syscalls+1
 752   .equ NR_syscalls,NR_syscalls+1
 753   .equ NR_syscalls,NR_syscalls+1
 754           .equ NR_syscalls,NR_syscalls+1
 755   .equ NR_syscalls,NR_syscalls+1
 756   .equ NR_syscalls,NR_syscalls+1
 757   .equ NR_syscalls,NR_syscalls+1
 758   .equ NR_syscalls,NR_syscalls+1
 759           .equ NR_syscalls,NR_syscalls+1
 760   .equ NR_syscalls,NR_syscalls+1
 761   .equ NR_syscalls,NR_syscalls+1
 762   .equ NR_syscalls,NR_syscalls+1
 763   .equ NR_syscalls,NR_syscalls+1
 764           .equ NR_syscalls,NR_syscalls+1
 765   .equ NR_syscalls,NR_syscalls+1
 766   .equ NR_syscalls,NR_syscalls+1
 767   .equ NR_syscalls,NR_syscalls+1
 768   .equ NR_syscalls,NR_syscalls+1
 769           .equ NR_syscalls,NR_syscalls+1
 770   .equ NR_syscalls,NR_syscalls+1
 771   .equ NR_syscalls,NR_syscalls+1
 772   .equ NR_syscalls,NR_syscalls+1
 773   .equ NR_syscalls,NR_syscalls+1
 774           .equ NR_syscalls,NR_syscalls+1
 775   .equ NR_syscalls,NR_syscalls+1
 776   .equ NR_syscalls,NR_syscalls+1
 777   .equ NR_syscalls,NR_syscalls+1
 778   .equ NR_syscalls,NR_syscalls+1
 779           .equ NR_syscalls,NR_syscalls+1
 780   .equ NR_syscalls,NR_syscalls+1
 781   .equ NR_syscalls,NR_syscalls+1
 782   .equ NR_syscalls,NR_syscalls+1
 783   .equ NR_syscalls,NR_syscalls+1
 784           .equ NR_syscalls,NR_syscalls+1
 785   .equ NR_syscalls,NR_syscalls+1
 786   .equ NR_syscalls,NR_syscalls+1
 787   .equ NR_syscalls,NR_syscalls+1
 788   .equ NR_syscalls,NR_syscalls+1
 789           .equ NR_syscalls,NR_syscalls+1
 790   .equ NR_syscalls,NR_syscalls+1
 791   .equ NR_syscalls,NR_syscalls+1
 792   .equ NR_syscalls,NR_syscalls+1
 793   .equ NR_syscalls,NR_syscalls+1
 794           .equ NR_syscalls,NR_syscalls+1
 795   .equ NR_syscalls,NR_syscalls+1
 796   .equ NR_syscalls,NR_syscalls+1
 797   .equ NR_syscalls,NR_syscalls+1
 798   .equ NR_syscalls,NR_syscalls+1
 799           .equ NR_syscalls,NR_syscalls+1
 800   .equ NR_syscalls,NR_syscalls+1
 801   .equ NR_syscalls,NR_syscalls+1
 802   .equ NR_syscalls,NR_syscalls+1
 803   .equ NR_syscalls,NR_syscalls+1
 804           .equ NR_syscalls,NR_syscalls+1
 805   .equ NR_syscalls,NR_syscalls+1
 806   .equ NR_syscalls,NR_syscalls+1
 807   .equ NR_syscalls,NR_syscalls+1
 808   .equ NR_syscalls,NR_syscalls+1
 809           .equ NR_syscalls,NR_syscalls+1
 810   .equ NR_syscalls,NR_syscalls+1
 811   .equ NR_syscalls,NR_syscalls+1
 812   .equ NR_syscalls,NR_syscalls+1
 813   .equ NR_syscalls,NR_syscalls+1
 814           .equ NR_syscalls,NR_syscalls+1
 815   .equ NR_syscalls,NR_syscalls+1
 816   .equ NR_syscalls,NR_syscalls+1
 817   .equ NR_syscalls,NR_syscalls+1
 818   .equ NR_syscalls,NR_syscalls+1
 819           .equ NR_syscalls,NR_syscalls+1
 820   .equ NR_syscalls,NR_syscalls+1
 821   .equ NR_syscalls,NR_syscalls+1
 822   .equ NR_syscalls,NR_syscalls+1
 823   .equ NR_syscalls,NR_syscalls+1
 824           .equ NR_syscalls,NR_syscalls+1
 825   .equ NR_syscalls,NR_syscalls+1
 826   .equ NR_syscalls,NR_syscalls+1
 827   .equ NR_syscalls,NR_syscalls+1
 828   .equ NR_syscalls,NR_syscalls+1
 829           .equ NR_syscalls,NR_syscalls+1
 830   .equ NR_syscalls,NR_syscalls+1
 831   .equ NR_syscalls,NR_syscalls+1
 832   .equ NR_syscalls,NR_syscalls+1
 833   .equ NR_syscalls,NR_syscalls+1
 834           .equ NR_syscalls,NR_syscalls+1
 835   .equ NR_syscalls,NR_syscalls+1
 836   .equ NR_syscalls,NR_syscalls+1
 837   .equ NR_syscalls,NR_syscalls+1
 838   .equ NR_syscalls,NR_syscalls+1
 839           .equ NR_syscalls,NR_syscalls+1
 840   .equ NR_syscalls,NR_syscalls+1
 841   .equ NR_syscalls,NR_syscalls+1
 842   .equ NR_syscalls,NR_syscalls+1
 843   .equ NR_syscalls,NR_syscalls+1
 844           .equ NR_syscalls,NR_syscalls+1
 845   .equ NR_syscalls,NR_syscalls+1
 846   .equ NR_syscalls,NR_syscalls+1
 847   .equ NR_syscalls,NR_syscalls+1
 848   .equ NR_syscalls,NR_syscalls+1
 849           .equ NR_syscalls,NR_syscalls+1
 850   .equ NR_syscalls,NR_syscalls+1
 851   .equ NR_syscalls,NR_syscalls+1
 852   .equ NR_syscalls,NR_syscalls+1
 853   .equ NR_syscalls,NR_syscalls+1
 854           .equ NR_syscalls,NR_syscalls+1
 855   .equ NR_syscalls,NR_syscalls+1
 856   .equ NR_syscalls,NR_syscalls+1
 857   .equ NR_syscalls,NR_syscalls+1
 858   .equ NR_syscalls,NR_syscalls+1
 859           .equ NR_syscalls,NR_syscalls+1
 860   .equ NR_syscalls,NR_syscalls+1
 861   .equ NR_syscalls,NR_syscalls+1
 862   .equ NR_syscalls,NR_syscalls+1
 863   .equ NR_syscalls,NR_syscalls+1
 864           .equ NR_syscalls,NR_syscalls+1
 865   .equ NR_syscalls,NR_syscalls+1
 866   .equ NR_syscalls,NR_syscalls+1
 867   .equ NR_syscalls,NR_syscalls+1
 868   .equ NR_syscalls,NR_syscalls+1
 869           .equ NR_syscalls,NR_syscalls+1
 870   .equ NR_syscalls,NR_syscalls+1
 871   .equ NR_syscalls,NR_syscalls+1
 872   .equ NR_syscalls,NR_syscalls+1
 873   .equ NR_syscalls,NR_syscalls+1
 874           .equ NR_syscalls,NR_syscalls+1
 875   .equ NR_syscalls,NR_syscalls+1
 876   .equ NR_syscalls,NR_syscalls+1
 877   .equ NR_syscalls,NR_syscalls+1
 878   .equ NR_syscalls,NR_syscalls+1
 879           .equ NR_syscalls,NR_syscalls+1
 880   .equ NR_syscalls,NR_syscalls+1
 881   .equ NR_syscalls,NR_syscalls+1
 882   .equ NR_syscalls,NR_syscalls+1
 883   .equ NR_syscalls,NR_syscalls+1
 884           .equ NR_syscalls,NR_syscalls+1
 885   .equ NR_syscalls,NR_syscalls+1
 886   .equ NR_syscalls,NR_syscalls+1
 887   .equ NR_syscalls,NR_syscalls+1
 888   .equ NR_syscalls,NR_syscalls+1
 889           .equ NR_syscalls,NR_syscalls+1
 890   .equ NR_syscalls,NR_syscalls+1
 891   .equ NR_syscalls,NR_syscalls+1
 892   .equ NR_syscalls,NR_syscalls+1
 893   .equ NR_syscalls,NR_syscalls+1
 894           .equ NR_syscalls,NR_syscalls+1
 895   .equ NR_syscalls,NR_syscalls+1
 896   .equ NR_syscalls,NR_syscalls+1
 897   .equ NR_syscalls,NR_syscalls+1
 898   .equ NR_syscalls,NR_syscalls+1
 899           .equ NR_syscalls,NR_syscalls+1
 900   .equ NR_syscalls,NR_syscalls+1
 901   .equ NR_syscalls,NR_syscalls+1
 902   .equ NR_syscalls,NR_syscalls+1
 903   .equ NR_syscalls,NR_syscalls+1
 904           .equ NR_syscalls,NR_syscalls+1
 905   .equ NR_syscalls,NR_syscalls+1
 906   .equ NR_syscalls,NR_syscalls+1
 907   .equ NR_syscalls,NR_syscalls+1
 908   .equ NR_syscalls,NR_syscalls+1
 909           .equ NR_syscalls,NR_syscalls+1
 910   .equ NR_syscalls,NR_syscalls+1
 911   .equ NR_syscalls,NR_syscalls+1
 912   .equ NR_syscalls,NR_syscalls+1
 913   .equ NR_syscalls,NR_syscalls+1
 914           .equ NR_syscalls,NR_syscalls+1
 915   .equ NR_syscalls,NR_syscalls+1
 916   .equ NR_syscalls,NR_syscalls+1
 917   .equ NR_syscalls,NR_syscalls+1
 918   .equ NR_syscalls,NR_syscalls+1
 919           .equ NR_syscalls,NR_syscalls+1
 920   .equ NR_syscalls,NR_syscalls+1
 921   .equ NR_syscalls,NR_syscalls+1
 922   .equ NR_syscalls,NR_syscalls+1
 923   .equ NR_syscalls,NR_syscalls+1
 924           .equ NR_syscalls,NR_syscalls+1
 925   .equ NR_syscalls,NR_syscalls+1
 926   .equ NR_syscalls,NR_syscalls+1
 927   .equ NR_syscalls,NR_syscalls+1
 928   .equ NR_syscalls,NR_syscalls+1
 929           .equ NR_syscalls,NR_syscalls+1
 930   .equ NR_syscalls,NR_syscalls+1
 931   .equ NR_syscalls,NR_syscalls+1
 932   .equ NR_syscalls,NR_syscalls+1
 933   .equ NR_syscalls,NR_syscalls+1
 934           .equ NR_syscalls,NR_syscalls+1
 935   .equ NR_syscalls,NR_syscalls+1
 936   .equ NR_syscalls,NR_syscalls+1
 937   .equ NR_syscalls,NR_syscalls+1
 938   .equ NR_syscalls,NR_syscalls+1
 939           .equ NR_syscalls,NR_syscalls+1
 940   .equ NR_syscalls,NR_syscalls+1
 941   .equ NR_syscalls,NR_syscalls+1
 942   .equ NR_syscalls,NR_syscalls+1
 943   .equ NR_syscalls,NR_syscalls+1
 944           .equ NR_syscalls,NR_syscalls+1
 945   .equ NR_syscalls,NR_syscalls+1
 946   .equ NR_syscalls,NR_syscalls+1
 947   .equ NR_syscalls,NR_syscalls+1
 948   .equ NR_syscalls,NR_syscalls+1
 949           .equ NR_syscalls,NR_syscalls+1
 950   .equ NR_syscalls,NR_syscalls+1
 951   .equ NR_syscalls,NR_syscalls+1
 952               .equ NR_syscalls,NR_syscalls+1
 953   .equ NR_syscalls,NR_syscalls+1
 954           .equ NR_syscalls,NR_syscalls+1
 955   .equ NR_syscalls,NR_syscalls+1
 956   .equ NR_syscalls,NR_syscalls+1
 957   .equ NR_syscalls,NR_syscalls+1
 958   .equ NR_syscalls,NR_syscalls+1
 959           .equ NR_syscalls,NR_syscalls+1
 960   .equ NR_syscalls,NR_syscalls+1
 961   .equ NR_syscalls,NR_syscalls+1
 962   .equ NR_syscalls,NR_syscalls+1
 963   .equ NR_syscalls,NR_syscalls+1
 964           .equ NR_syscalls,NR_syscalls+1
 965   .equ NR_syscalls,NR_syscalls+1
 966   .equ NR_syscalls,NR_syscalls+1
 967   .equ NR_syscalls,NR_syscalls+1
 968   .equ NR_syscalls,NR_syscalls+1
 969           .equ NR_syscalls,NR_syscalls+1
 970   .equ NR_syscalls,NR_syscalls+1
 971   .equ NR_syscalls,NR_syscalls+1
 972   .equ NR_syscalls,NR_syscalls+1
 973   .equ NR_syscalls,NR_syscalls+1
 974           .equ NR_syscalls,NR_syscalls+1
 975   .equ NR_syscalls,NR_syscalls+1
 976   .equ NR_syscalls,NR_syscalls+1
 977   .equ NR_syscalls,NR_syscalls+1
 978   .equ NR_syscalls,NR_syscalls+1
 979           .equ NR_syscalls,NR_syscalls+1
 980   .equ NR_syscalls,NR_syscalls+1
 981   .equ NR_syscalls,NR_syscalls+1
 982   .equ NR_syscalls,NR_syscalls+1
 983   .equ NR_syscalls,NR_syscalls+1
 984           .equ NR_syscalls,NR_syscalls+1
 985   .equ NR_syscalls,NR_syscalls+1
 986   .equ NR_syscalls,NR_syscalls+1
 987   .equ NR_syscalls,NR_syscalls+1
 988   .equ NR_syscalls,NR_syscalls+1
 989           .equ NR_syscalls,NR_syscalls+1
 990   .equ NR_syscalls,NR_syscalls+1
 991   .equ NR_syscalls,NR_syscalls+1
 992   .equ NR_syscalls,NR_syscalls+1
 993   .equ NR_syscalls,NR_syscalls+1
 994           .equ NR_syscalls,NR_syscalls+1
 995   .equ NR_syscalls,NR_syscalls+1
 996   .equ NR_syscalls,NR_syscalls+1
 997   .equ NR_syscalls,NR_syscalls+1
 998   .equ NR_syscalls,NR_syscalls+1
 999           .equ NR_syscalls,NR_syscalls+1
1000   .equ NR_syscalls,NR_syscalls+1
1001   .equ NR_syscalls,NR_syscalls+1
1002   .equ NR_syscalls,NR_syscalls+1
1003   .equ NR_syscalls,NR_syscalls+1
1004           .equ NR_syscalls,NR_syscalls+1
1005   .equ NR_syscalls,NR_syscalls+1
1006   .equ NR_syscalls,NR_syscalls+1
1007   .equ NR_syscalls,NR_syscalls+1
1008   .equ NR_syscalls,NR_syscalls+1
1009           .equ NR_syscalls,NR_syscalls+1
1010   .equ NR_syscalls,NR_syscalls+1
1011   .equ NR_syscalls,NR_syscalls+1
1012   .equ NR_syscalls,NR_syscalls+1
1013   .equ NR_syscalls,NR_syscalls+1
1014           .equ NR_syscalls,NR_syscalls+1
1015   .equ NR_syscalls,NR_syscalls+1
1016   .equ NR_syscalls,NR_syscalls+1
1017   .equ NR_syscalls,NR_syscalls+1
1018   .equ NR_syscalls,NR_syscalls+1
1019           .equ NR_syscalls,NR_syscalls+1
1020   .equ NR_syscalls,NR_syscalls+1
1021   .equ NR_syscalls,NR_syscalls+1
1022   .equ NR_syscalls,NR_syscalls+1
1023   .equ NR_syscalls,NR_syscalls+1
1024           .equ NR_syscalls,NR_syscalls+1
1025   .equ NR_syscalls,NR_syscalls+1
1026   .equ NR_syscalls,NR_syscalls+1
1027   .equ NR_syscalls,NR_syscalls+1
1028 
1029 .equ syscalls_padding, ((NR_syscalls + 3) & ~3) - NR_syscalls
1030 
1031 
1032 .rept syscalls_padding
1033   .equ NR_syscalls,NR_syscalls+1
1034 .endr
1035 # 102 "arch/arm/kernel/entry-common.S" 2
1036 
1042 
1043 .ifne NR_syscalls - (388)
1044 .error "__NR_syscalls is not equal to the size of the syscall table"
1045 .endif
1046 # 122 "arch/arm/kernel/entry-common.S"
1047  .align 5
1048 .globl vector_swi ; .align 0 ; vector_swi:
1049 

cpp.sh
1 arm-linux-gnueabihf-gcc -Wp,-MD,arch/arm/kernel/.entry-common.o.d  -nostdinc -isystem /media/work/altera/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/../lib/gcc/arm-linux-gnueabihf/4.9.2/include -I./arch/arm/include -Iarch/arm/include/generated/uapi -Iarch/arm/include/generated  -Iinclude -I./arch/arm/include/uapi -Iarch/arm/include/generated/uapi -I./include/uapi -Iinclude/generated/uapi -include ./include/linux/kconfig.h -D__KERNEL__ -mlittle-endian -Iarch/arm/mach-bcm2709/include   -D__ASSEMBLY__ -mabi=aapcs-linux -mno-thumb-interwork -mfpu=vfp -funwind-tables -marm -D__LINUX_ARM_ARCH__=7 -march=armv7-a  -include asm/unified.h -msoft-float  -DCC_HAVE_ASM_GOTO            -E arch/arm/kernel/entry-common.S > /tmp/a.c

2 arm-linux-gnueabihf-as -c /tmp/a.c
3 arch/arm/kernel/entry-common.S: Assembler messages:
4 arch/arm/kernel/entry-common.S:110: Error: __NR_syscalls is not equal to the size of the syscall table

add_arm_linux_new_syscall.patch L113 我用了可以傳入一個參數的 macro, 可以參考其它的 system call 看看如何使用不同參數的版本, ex: SYSCALL_DEFINE3。system call 並不是表面上看的那麼單純, 我認為只有實作過才真的能理解它, 否則你只是停留在理論的理解。

test_my_syscall.c 用來呼叫這個 new system call, 這是把 mmu ttbr0 印出來, 我本來想解開 linux mmu 位址轉換的祕密, 很可惜失敗了。

test_my_syscall.c
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <unistd.h>
 4 #include <errno.h>
 5 typedef unsigned int u32;
 6 
 7 int my_syscall(int reg_type)
 8 {     
 9   printf("user mode ## reg_type : %d\n", reg_type);
10   return (long) syscall(388, reg_type);
11 } 
12 
13 int main(int argc, char *argv[])
14 {
15   int reg_type=0;
16 
17   if (argc >= 2)
18     reg_type = strtol(argv[1], NULL, 10);
19 
20   printf("page size: %d\n", getpagesize());
21 
22   u32 ttbr0 = my_syscall(reg_type);
23   if (ttbr0 == -1)
24   {
25     perror("err");
26   }
27   printf("reg: %x\n", ttbr0);
28   return 0;
29 }

沒有留言:

張貼留言

使用 google 的 reCAPTCHA 驗證碼, 總算可以輕鬆留言了。

我實在受不了 spam 了, 又不想讓大家的眼睛花掉, 只好放棄匿名留言。這是沒辦法中的辦法了。留言的朋友需要有 google 帳號。