TSysCallArg

This code provides helper functions for retrieving arguments from and setting values into the uctx_pool structure for the current running process. These functions are crucial for implementing system calls, enabling user processes to interact with the kernel and receive feedback.

Header Files

#include <lib/trap.h>
#include <lib/x86.h>
#include "import.h"
  • <lib/trap.h>: Provides definitions and utilities for handling traps (interrupts or exceptions) in the system.

  • <lib/x86.h>: Contains architecture-specific constants, macros, and utility functions for x86 systems.

  • "import.h": Imports project-specific constants, structures, and external function declarations.

Global Declarations

extern tf_t uctx_pool[NUM_IDS];
  • Purpose: Declares uctx_pool as an array of trap frames, indexed by process IDs. Each entry in uctx_pool stores the CPU register states (regs) for a specific process.

  • Use Case: This is used to maintain the state of user processes during context switches or system calls.

Argument Retrieval Functions

syscall_get_arg1

unsigned int syscall_get_arg1(void)
{
    unsigned int process_id = get_curid();
    return uctx_pool[process_id].regs.eax;
}
  • Purpose: Retrieves the first argument (eax) passed by the current process during a system call.

  • How It Works:

    1. Fetches the current process ID using get_curid().

    2. Accesses eax in the regs structure of the trap frame for the current process.

  • Use Case: Used by the kernel to determine the system call type or retrieve the first argument passed by the user process.

Similar Functions

These functions follow the same logic but retrieve arguments from different registers:

  • syscall_get_arg2: Retrieves the second argument from ebx.

  • syscall_get_arg3: Retrieves the third argument from ecx.

  • syscall_get_arg4: Retrieves the fourth argument from edx.

  • syscall_get_arg5: Retrieves the fifth argument from esi.

  • syscall_get_arg6: Retrieves the sixth argument from edi.

Setting Error Number

syscall_set_errno

void syscall_set_errno(unsigned int errno)
{
    unsigned int process_id = get_curid();
    uctx_pool[process_id].regs.eax = errno;
}
  • Purpose: Sets the error number (errno) for the current process. The value is written to the eax register in the trap frame.

  • How It Works:

    1. Identifies the current process ID.

    2. Writes the error number into eax of the process's trap frame.

  • Use Case: Returns an error code to the user process when a system call fails.

Setting Return Values

syscall_set_retval1

void syscall_set_retval1(unsigned int retval)
{
    unsigned int process_id = get_curid();
    uctx_pool[process_id].regs.ebx = retval;
}
  • Purpose: Sets the first return value (retval) for the current process in the ebx register.

  • Use Case: Returns results from system calls to the user process.

Similar Functions

These functions set return values in different registers:

  • syscall_set_retval2: Writes to ecx.

  • syscall_set_retval3: Writes to edx.

  • syscall_set_retval4: Writes to esi.

  • syscall_set_retval5: Writes to edi.

Purpose and Use Cases

  1. System Call Management:

    • Retrieve arguments passed by user processes.

    • Return results or error codes back to user processes.

  2. Trap Frame Manipulation:

    • Modify and inspect uctx_pool to ensure proper communication between user and kernel modes.

  3. Context Management:

    • Helps maintain isolation between processes while enabling controlled interaction through system calls.

  4. Scalability:

    • Supports up to six arguments and return values, covering most system call requirements.

Last updated