fix merge conflicts

This commit is contained in:
EDiasAlberto
2024-11-04 01:00:33 +00:00

View File

@@ -13,7 +13,7 @@ static void syscall_handler (struct intr_frame *);
in size. */
typedef uintptr_t (*syscall_function) (uintptr_t, uintptr_t, uintptr_t);
/* System Call Functions */
/* System call function prototypes */
static void halt (void);
static void exit (int status);
static pid_t exec (const char *cmd_line);
@@ -30,14 +30,17 @@ static void close (int fd);
static void *validate_user_pointer (void *ptr, size_t size);
/* A struct defining a pair of a syscall_function along with its arity. */
typedef struct {
syscall_function function;
int arity;
/* A struct defining a syscall_function pointer along with its arity. */
typedef struct
{
syscall_function function; /* Function pointer. */
int arity; /* Number of arguments of the function. */
} syscall_arguments;
/* A look-up table for system call functions mapped using their numbers. */
static const syscall_arguments syscall_lookup[] = {
/* A look-up table mapping numbers to system call functions with their number of
arguments. */
static const syscall_arguments syscall_lookup[] =
{
[SYS_HALT] = {(syscall_function) halt, 0},
[SYS_EXIT] = {(syscall_function) exit, 1},
[SYS_EXEC] = {(syscall_function) exec, 1},
@@ -53,7 +56,9 @@ static const syscall_arguments syscall_lookup[] = {
[SYS_CLOSE] = {(syscall_function) close, 1},
};
static const int lookup_size
/* The number of syscall functions (i.e, number of elements) within the
syscall_lookup table. */
static const int LOOKUP_SIZE
= sizeof (syscall_lookup) / sizeof (syscall_arguments);
void
@@ -65,20 +70,25 @@ syscall_init (void)
static void
syscall_handler (struct intr_frame *f)
{
/* First, read the system call number from the stack. */
validate_user_pointer (f->esp, 1);
int syscall_number = *(int *) f->esp;
unsigned syscall_number = *(int *) f->esp;
if (syscall_number < 0 || syscall_number >= lookup_size)
/* Ensures the number corresponds to a system call that can be handled. */
if (syscall_number >= LOOKUP_SIZE)
thread_exit ();
syscall_arguments syscall = syscall_lookup[syscall_number];
validate_user_pointer (f->esp, syscall.arity);
uintptr_t args[3];
/* Next, read and copy the arguments from the stack pointer. */
validate_user_pointer (f->esp + sizeof (uintptr_t),
syscall.arity * sizeof (uintptr_t));
uintptr_t args[3] = {0};
for (int i=0; i < syscall.arity; i++)
args[i] = *(uintptr_t *) (f->esp + 1 + i);
args[i] = *(uintptr_t *) (f->esp + sizeof (uintptr_t) * (i + 1));
/* Call the function that handles this system call with the arguments. When
there is a return value it is stored in f->eax. */
f->eax = syscall.function (args[0], args[1], args[2]);
}
@@ -89,7 +99,7 @@ halt (void)
}
static void
exit (int status)
exit (int status UNUSED)
{
//TODO
}
@@ -169,12 +179,16 @@ close (int fd)
//TODO
}
/* A function to validate if a block of memory starting at PTR and of
size SIZE bytes is fully contained within user virtual memory. */
/* Validates if a block of memory starting at PTR and of size SIZE bytes is
fully contained within user virtual memory. Kills the thread (by calling
thread_exit) if the memory is invalid. Otherwise, returns the PTR given.
If the size is 0, the function does no checks and returns PTR.*/
static void *
validate_user_pointer (void *ptr, size_t size)
{
if (ptr == NULL || !is_user_vaddr (ptr) || !is_user_vaddr (ptr + size - 1))
if (size > 0 && (ptr == NULL ||
!is_user_vaddr (ptr) ||
!is_user_vaddr (ptr + size - 1)))
thread_exit ();
return ptr;