๊ด€๋ฆฌ ๋ฉ”๋‰ด

๋ง๊ฐ๋กœ๊ทธ

ํฌ๋ž˜ํ”„ํ†ค ์ •๊ธ€ WEEK08 DAY69 - PintOS Project2 system call - halt,exit,wrtie,create ๋ณธ๋ฌธ

Krafton jungle

ํฌ๋ž˜ํ”„ํ†ค ์ •๊ธ€ WEEK08 DAY69 - PintOS Project2 system call - halt,exit,wrtie,create

habbn 2024. 3. 16. 22:59
728x90
๐Ÿ“†2024.3.16

1. system call ๊ตฌํ˜„ - halt, exit, write, create

 

System Calls ๊ตฌํ˜„

 

์‹œ์Šคํ…œ ์ฝœ ํ•ธ๋“ค๋Ÿฌ์ธ syscall_handler()๊ฐ€ ์ œ์–ด๊ถŒ์„ ์–ป์œผ๋ฉด, ์‹œ์Šคํ…œ ์ฝœ ๋ฒˆํ˜ธ๊ฐ€ %rax์— ์žˆ์œผ๋ฉฐ, ์ธ์ž๊ฐ’๋“ค์€ ์ˆœ์„œ๋Œ€๋กœ ์ „๋‹ฌ๋œ๋‹ค. (%rdi, %rsi, %rdx, %r10, %r8 and %r9)

ํ˜ธ์ถœ์ž์˜ ๋ ˆ์ง€์Šคํ„ฐ๋Š” struct intr_frame์œผ๋กœ ์ „๋‹ฌ๋˜์–ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.(struct infr_frame์€ ์ปค๋„ ์Šคํƒ์— ์žˆ๋‹ค.)

 

์œ ์ € ํ”„๋กœ๊ทธ๋žจ์˜ ํ”„๋กœํ† ํƒ€์ž…์€ include/lib/user/syscall.h ๋‹ด๊ฒจ ์žˆ๋‹ค. (์ด๊ณณ์— ํ—ค๋” ํŒŒ์ผ๊ณผ ๋‹ค๋ฅธ include/lib/user์— ์žˆ๋Š” ํŒŒ์ผ๋“ค์€ ์œ ์ € ํ”„๋กœ๊ทธ๋žจ์—์„œ๋งŒ ์‚ฌ์šฉ๋œ๋‹ค.) 

 

-> user/syscall.c ์—์„œ syscall_handler()๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ์‹œ์Šคํ…œ ์ฝœ ๋ฒˆํ˜ธ, ์ธ์ž๋“ค์„ ๋ฐ›์•„์™€์„œ ๊ทธ์— ์•Œ๋งž์€ ์•ก์…˜์„ ์ทจํ•˜๋„๋ก ์‹œ์Šคํ…œ ์ฝœ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

 

 

1. halt

src/include/threads/init.h์— ์„ ์–ธ๋œ power_off()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ํ•€ํ† ์Šค๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค.

-> #include "threads/init.h" ์ถ”๊ฐ€ํ•ด์•ผ ๋จ

void halt (void) 
{	
    power_off();
}

 

 

2. exit

ํ˜„์žฌ ๋™์ž‘ ์ค‘์ธ ์œ ์ € ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒ์‹œํ‚ค๊ณ , ์ปค๋„๋กœ ์ƒํƒœ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋งŒ์•ฝ ํ”„๋กœ์„ธ์Šค์˜ ๋ถ€๋ชจ๊ฐ€ wait ์ƒํƒœ๋ผ๋ฉด , wait์— ๋“ค์–ด๊ฐˆ ์ƒํƒœ๋ฅผ ๋ฐ˜ํ™˜ํ•  ๊ฒƒ์ด๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ƒํƒœ๊ฐ€ 0์ด๋ฉด ์„ฑ๊ณต, 0์ด ์•„๋‹Œ ์ˆ˜๋Š” ์—๋Ÿฌ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

void exit (int status)
{	
	struct thread *cur = thread_current();
	printf("%s: exit(%d)\n", cur->name, status);
	thread_exit();
}

 

3. write

buffer๋กœ๋ถ€ํ„ฐ open file fd๋กœ size ๋ฐ”์ดํŠธ๋ฅผ ์ ์–ด์ค€๋‹ค. ์‹ค์ œ๋กœ ์ ํžŒ ๋ฐ”์ดํŠธ์˜ ์ˆ˜ ๋ฐ˜ํ™˜ํ•ด ์ฃผ๊ณ , ์ผ๋ถ€ ๋ฐ”์ดํŠธ๊ฐ€ ์ ํžˆ์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด size๋ณด๋‹ค ๋” ์ž‘์€ ๋ฐ”์ดํŠธ ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜๋  ์ˆ˜ ์žˆ๋‹ค. ๋” ์ด์ƒ ๋ฐ”์ดํŠธ๋ฅผ ์ ์„ ์ˆ˜ ์—†๋‹ค๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

putbuf()๋ฅผ ํ†ตํ•ด ํ•œ๋ฒˆ์— ํ˜ธ์ถœํ•˜์—ฌ ๋ชจ๋“  ๋‚ด์šฉ์„ ๋ฒ„ํผ์— ๋‹ด๋Š”๋‹ค. 

int write (int fd, const void *buffer, unsigned length)
{
	int byte = 0;
	if(fd == 1)
	{
		putbuf(buffer, length);
		byte = length;
	}
	return byte;
}

 

4. create

file์„ ์ด๋ฆ„์œผ๋กœ ํ•˜๊ณ  ํฌ๊ธฐ๊ฐ€ inital_size์ธ ์ƒˆ๋กœ์šด ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค. ์„ฑ๊ณต์ ์œผ๋กœ ํŒŒ์ผ ์ƒ์„ฑํ•˜๋ฉด true, ์‹คํŒจ false ๋ฐ˜ํ™˜ํ•œ๋‹ค.

bool create (const char *file, unsigned initial_size)
{
	if(!check_address(file))
		return filesys_create (file, initial_size);
}

 

์ฃผ์–ด์ง„ ํฌ์ธํ„ฐ ์œ ํšจํ•œ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€์ง€๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด  check_address() ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜์˜€๋‹ค.

๋งŒ์ผ ์œ ํšจํ•œ ์ฃผ์†Œ๊ฐ’์ด๋ฉด filesys_create()๋กœ ํŒŒ์ผ ์ƒ์„ฑํ•˜๋„๋ก ํ•˜์˜€๋‹ค.

 

void check_address(void *file_addr)
{
	struct thread *t = thread_current();
	if(pml4_get_page(t->pml4, file_addr) == NULL || !is_user_vaddr(file_addr) || file_addr == NULL)
		exit(-1);
}

 

1. ํŽ˜์ด์ง€ ํ…Œ์ด๋ธ”์„ ํ†ตํ•ด ์ฃผ์–ด์ง„ ์ฃผ์†Œ๊ฐ€ ์œ ํšจํ•œ์ง€ ํ™•์ธํ•œ๋‹ค. 

2. ์ฃผ์–ด์ง„ ์ฃผ์†Œ๊ฐ€ ์œ ํšจํ•œ ์‚ฌ์šฉ์ž ์˜์—ญ ์ฃผ์†Œ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.

3. file_addr์ด NULL์ธ์ง€ ํ™•์ธํ•˜์—ฌ ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•œ๋‹ค. 

 

pml4(page map level 4)

x86 ์•„ํ‚คํ…์ฒ˜์—์„œ ๊ฐ€์ƒ ๋ฉ”๋ชจ๋ฆฌ์™€ ๋ฌผ๋ฆฌ ๋ฉ”๋ชจ๋ฆฌ ๊ฐ„์˜ ๋งคํ•‘์„ ์ œ์–ดํ•˜๋Š” ํŽ˜์ด์ง€ ํ…Œ์ด๋ธ”์˜ ์ตœ์ƒ์œ„ ๋ ˆ๋ฒจ์ด๋‹ค.

 


 

 

์˜ค๋Š˜์€ ์—ฌ๊ธฐ๊นŒ์ง€ ๊ตฌํ˜„ํ–ˆ๋‹ค. ๊ฐ™์€ ํŒ€์› ๋ถ„๋“ค์ด ๋‹ค ์•„ํ”„์…”์„œ ์˜ค๋Š˜ ์†”ํ”Œ ํ–ˆ๋”๋‹ˆ ๋ญ˜ ํ•ด์•ผ ๋ ์ง€ ๊ฐ์ด ํ•˜๋‚˜๋„ ์•ˆ ์žกํ˜€์žˆ์—ˆ๋Š”๋ฐ ๋งŒ์„๋‹˜์ด ์ €ํฌ ์กฐ ํ•˜๋Š” ๊ฑฐ ๋ณด์‹ค๋ž˜์š”? ํ•˜์…”์„œ ๋’ค์—์„œ ์ฒญ๊ฐ•ํ•˜๋‹ค ๋ณด๋‹ˆ ์•„ํ•˜! ์•„ํ•˜! ์•„ํ•˜ 3๋ฒˆ ์™ธ์นจ

๊ทธ๋ž˜์„œ ๋ฐ”๋กœ ์ž๋ฆฌ ๊ฐ€์„œ ํ–ˆ๋”๋‹ˆ pass pass pass..

๊ทผ๋ฐ ์ด๊ฑธ ์ข‹์•„ํ•ด์•ผ ํ• ์ง€ ๋‚ด๊ฐ€ ํ•œ ๊ฒŒ ์•„๋‹ˆ๋ผ ์–ด์ฐŒ ๋ณด๋ฉด ๋‹ต์•ˆ์ง€๋ฅผ ๋ณด๊ณ  ๊ณ ๋Œ€๋กœ ์ ์–ด์„œ ํ•œ ๊ฑฐ๋‹ˆ๊นŒ.. ๊ทธ๋ž˜๋„ ๋งŒ์„๋‹˜ ๋•๋ถ„์— ๊ธธ์ด ํŠธ์˜€์œผ๋‹ˆ ์ด์ œ๋Š” ๋‹ค์‹œ ์Šค์Šค๋กœ ํ•ด์•ผ๊ฒ ๋‹ค!

728x90