0% found this document useful (0 votes)
16 views

OS Project2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

OS Project2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Report for Project -

Operating System
System calls

Teacher Assistant: M.S. Nguyễn Trung Quân,


M.S. Chung Thùy Linh
Group: Nguyễn Quang Thịnh (22120346),
Trần Quốc Duy (22120082)

Content

1 Introduction 1
1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Project Notes . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Work Distribution . . . . . . . . . . . . . . . . . . . . . . 1
1.4 Development Environment . . . . . . . . . . . . . . . . . 1

2 Solutions 1
2.1 Using GDB . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2.2 System Call . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.3 System Call . . . . . . . . . . . . . . . . . . . . . . . . . . 2

3 References 4

Hồ Chí Minh City – 2024


1 Introduction

1.1 Overview
This project focuses on implementing and understanding system calls in the xv6 operating
system. The main tasks involve adding new system calls and understanding kernel debugging
using GDB. The project aims to deepen our understanding of kernel internals and system call
mechanisms.

1.2 Project Notes


All implementations use standard xv6 system calls and kernel modifications. The work follows
the course materials and xv6 documentation guidelines for system call implementation.

1.3 Work Distribution


The work was divided fairly as follows:

Team Member Tasks Completion Status


Trần Quốc Duy GDB exercises, System call tracing Excellently completed
Nguyễn Quang Thịnh GDB exercises, Sysinfo implementation Excellently completed

1.4 Development Environment


• Operating System: Linux
• Framework: xv6 operating system
• Tool: GDB debugger

2 Solutions

2.1 Using GDB


Our answers is in file answer_syscall.txt
In the problem requires us to determine the value stored in p->trapframe->a7, by using GDB
and examining the syscall.h file, we can identify the system call being executed.
Through GDB debugging, we observed:
(gdb) p num
$1 = 7
This value 7 is obtained from p->trapframe->a7 as shown in the syscall function. Referring to
syscall.h, we can see that this corresponds to:
#define SYS_exec 7
In the problem that we need to determine the previous mode of the CPU, we check this by
examining the SPP (Supervisor Previous Privilege) bit, which is the 8th bit from the right in
the sstatus register.[2]. Through GDB debugging, we can check the SPP value using:
(gdb) p ($sstatus >> 8) & 1
$1 = 0
When SPP equals 0, this indicates that the CPU was previously in user mode.

1
2.2 System Call
To add a new system call named trace, we followed these steps [1].

1. Add a prototype to user/user.h:

int trace(int);

2. Add a stub to user/usys.pl:

entry("trace");

3. Add a syscall number to kernel/syscall.h:

#define SYS_trace 22

4. Add the new syscall into kernel/syscall.c:

extern uint64 sys_trace(void);


...
[SYS_trace] sys_trace,

5. Add sys_trace function in kernel/sysproc.c:

uint64
sys_trace(void)
{
int mask;
argint(0, &mask);
struct proc *p = myproc();
p->trace_mask = mask;
return 0;
}

2.3 System Call


To add a new system call named sysinfo, we followed these steps [1].

1. Add a prototype to user/user.h:

int sysinfo(struct sysinfo *);

2. Add a stub to user/usys.pl:

entry("sysinfo");

3. Add a syscall number to kernel/syscall.h:

#define SYS_sysinfo 22

4. Add the new syscall into kernel/syscall.c:

2
extern uint64 sys_sysinfo(void);
...
[SYS_sysinfo] sys_sysinfo,

5. Add sys_sysinfo function in kernel/sysproc.c:

#include "sysinfo.h"

uint64
sys_sysinfo(void)
{
struct sysinfo info;
uint64 addr;
struct proc *p = myproc();

argaddr(0, &addr);

// Fill in the sysinfo structure


info.nproc = nproc();
info.freemem = freemem();
info.loadavg = average_load();

// Copy the sysinfo structure back to userspace


if (copyout(p->pagetable, addr, (char *)&info, sizeof(info)) < 0) {
return -1;
}
return 0;
}

6. Add sysinfo struct in kernel/sysinfo.h:

struct sysinfo {
uint64 freemem;
uint64 nproc;
uint64 loadavg;
};

7. Implement the freemem function in kernel/kalloc.c:

uint64
freemem(void)
{
uint64 free_mem = 0;
struct run *r;

acquire(&kmem.lock);
r = kmem.freelist;
for (; r; r = r->next) {
free_mem += PGSIZE;
}

3
release(&kmem.lock);

return free_mem;
}

8. Implement the nproc and average_load functions in kernel/proc.c:

uint64
nproc(void)
{
uint64 num = 0;
struct proc *p;

for (p = proc; p < &proc[NPROC]; p++) {


if (p->state != UNUSED)
num++;
}

return num;
}

uint64
average_load(void)
{
uint64 num_run = 0;
uint64 total = 0;
struct proc *p;

for (p = proc; p < &proc[NPROC]; p++) {


if (p->state != UNUSED) {
total++;
if (p->state == RUNNABLE) {
num_run++;
}
}
}

return ((total * 100) / num_run);


}

3 References
1. Instructions from: https://drive.google.com/drive/folders/1ySOIPYnQiUzBhAoKoZ_
dXb_Y0kOL22GH

2. ’The RISC-V Instruction Set Manual Volume II: Privileged Architecture’

You might also like