Quality control (1)/Operating System

OS week4 interrupt / system call

빈그레 2022. 10. 9. 21:58

 


system_call /  interrupt(2)

 

 

[os week3 interrupt 이어서..]

 

7-1) sys_call_table[] is in arch/x86/kernel/syscall_table_32.S. How many system calls does Linux 2.6 support?

 

 

0부터 시작했으므로 linux 2.6이 제공하는 system calls는 총 326개이다.

 

7-2)What are the system call numbers for exit, fork, execve, wait4, read, write, and mkdir?

 

파일 내에서 "/찾고있는text"+enter를 통해 각 system call의 number를 찾는다.

 

 

 

 

 

 

 

 

 

Role of sys_ni_syscall 

 

7-3) Find system call numbers for sys_ni_syscall, which is defined at kernel/sys_ni.c.

What is the role of sys_ni_syscall?

 

 

sys_ni_syscall 이라는 system call은 파일 내에 여러개 존재한다.

 

 

 

문제에 제시된 sys_ni_syscall의 위치로 들어가서 코드를 확인하였다.

"-ENOSYS"로 아무 역할도 수행하지 않는 함수였다.

 

 

 

(*문제에 제시된 주소의 kernel은 arch의 하위 directory에 있는 kernel이 아니고

linux-2.6의 하위 directory에 있는 kernel이다.)

 

 

 

8) Change the kernel such that it prints "length 17 string found" for each printf(s) when the length of s is 17.

Run a program that contains a printf() statement to see the effect. printf(s) calls write(1, s, strlen(s)) system call which in turn runs

 

mov eax, 4 ; eax<--4. 4 is system call number for “write”
int 128
INT 128 will make the cpu stop running current process and jump to the location written in IDT[128]. IDT[128] contains the address of system_call (located in arch/x86/kernel/entry_32.S). Finally, system_call will execute
call *sys_call_table(,%eax,4)
which eventually calls sys_write() since eax=4 for write() system call (the target function location is sys_call_table+eax*4).
* Sometimes the the system call runs "sysenter" instead of "int 128". In this case the cpu jumps to ia32_sysenter_target (also in entry_32.S) instead of system_call.

 

system call 함수 수정

 

system call 함수 수정시 fs/read_write.c내에서 함수 찾아서 수정한다.

 

 

 

modification sys_write

 

 

string의 count가 17이 되면 즉 printf에 들어오는 string이 17글자가 되면

"length 17 string found"를 출력하도록 system call함수 sys_write를 수정하였다.

 

 

 

recompile과 reboot를 진행한 뒤에 sys_write함수가 정상적으로 수정되었는지를 확인하기 위하여

root위치에서 새로운 code file을 만들어서 17글자를 출력하도록 하였다.

(새 파일 생성시 root 위치에서 만든다)

 

 

 

 

그냥 실행했을 때에는 "length 17 string found"가 출력되지 않았다.

 

console의 log level이 더 높으면 printk()는 console에 message를 띄울 수 없다.

printk()가 console에 메세지를 display할 수 있도록 echo를 통해 console의 log level을 가장 낮게 8로 설정해준다.

 

이후 다시 파일을 실행했을 때에는 17글자 입력에 대한 "length 17 string found"가 출력되었다.

 

 

9) You can call a system call indirectly with “syscall()”.

       write(1, “hi”, 2);

can be written as

      syscall(4, 1, “hi”, 2); // 4 is the system call number for “write” system call

Write a program that prints “hello” in the screen using syscall.

 

 

write(system_call finction)에 대한 call number를 syscall함수에 입력하여 write를 호출하였다.

 

 

 

 

write 함수에 대한 직접적인 호출 없이 syscall만으로도 write함수가 호출되어

정상적으로 코드가 수행되었음을 확인할 수 있었다.

 

 

 

 

10) Create a new system call, my_sys_call with system call number 17 (system call number 17 is one that is not being used currently). Define my_sys_call() just before sys_write() in read_write.c. Write a program that uses this system call:

 

void main(){

syscall(17); // calls a system call with syscall number 17

}

 

When the above program runs, the kernel should display hello from my_sys_call

 

 

 

cread new sys_call

 

To define a new system call with syscall number x
- insert the new system call name in arch/x86/kernel/syscall_table_32.S at index x
- define the function in appropriate file (such as "read_write.c")
asmlinkage void my_sys_call(){
printk("hello from my_sys_call\n");
}​

 

 

- recompile and reboot -> runs

 

To use this system call in a user program

- void main(){

syscall(x);

}

 

 

 

- insert the new system call name in arch/x86/kernel/syscall_table_32.S at index 17

 

 

arch/x86/kernel/syscall_table_32.S 로 들어와서 syscall17을 my_sys_call로 변경해주었다.

 

 

 

- define the function in appropriate file (such as "read_write.c")

 

 

my_sys_call 생성을 위하여 fs/read_write.c 로 들어왔다.

 

 

 

 

my_sys_call 정의를 추가하고 recompile,reboot를 진행했다.

 

 

 

syscall함수로 my_sys_call함수를 호출하는 ex2.c를 생성하였다.

 

 

 

printk를 이용하므로 console의 log level을 8로 변경해주고 run ex2했을 때 my_sys_call함수가 제대로 호출되어

 함수에 printk로 넣었던 문장이 출력되었다.

 

 

 

 

10-1) Create another system call that will add two numbers given by the user.

ex0.c:

void main(){

int sum;

sum = syscall(31, 4, 9); // suppose 31 is an empty entry in sys_call_table

printf("sum is %d\n, sum);

}

#./ex0

sum is 13

:ㅈㅂ

먼저 syscall_table_32.S에서 sysnumber 31을 수정해주고 recompile reboot진행한다.

 

 

 

read_write.c파일에 my_add_sys 함수를 정의해주었다.

아니 근데ㅣ,,,printk 쓰니까 자꾸 length 17,,저번에 했던 거 떠서 그것 좀 없애버려야겠음 열받아.

 

 

 

 

system call number 31인 함수를 호출하는 ex2파일을 생성하였다.

 

syscall의 첫번째 인자는 system call number를 의미하고

나머지 인자들은 syscall을 통해 호출하는 sys_call함수의 인자를 의미한다.

 

 

 

ex2파일의 syscall함수의 인자값으로 넣었던 정수들의 덧셈이 정상적으로 이루어졌음을 확인할 수 있었다.

 

 

 

11) Modify the kernel such that it displays the system call number for all system calls. Run a simple program that displays "hello" in the screen and find out what system calls have been called. Also explain for each system call why that system call has been used.

 

먼저 쓰이지 않는 sys call number을 사용하여 my_sys_call_set, my_sys_call_number 의 syscall number를 지정한다.

(본인은 32,35로 하였음)

 

 

read_write.c에 함수를 정의하고 recompile, reboot를 진행한다.

 

 

 

entry_32.S를 수정해주었다.

 

함수..실행해봐야하는데...너무 귀차나,,,,, 나중에 할래.

 

 

12) What system calls are being called when you remove a file? Use "system()" function to run a Linux command as below. Explain what each system call is doing. You need to make f1 file before you run it. Also explain for each system call why that system call has been used.

 

...........

system("rm f1");

...........

13) Find rm.c in busybox-1.31.1 and show the code that actually removes "f1". Note all linux commands are actually a program, and running "rm" command means running rm.c program. "rm" needs a system call defined in uClibc-0.9.33.2 to remove a file. You may want to continue the code tracing all the way up to "INT 0x80" in uClibc for this system call.