Running a user program
XSM Unprivileged Mode Execution
The first user program executed is called the INIT program, stored in blocks 7-8 (INIT/LOGIN Code)
In this stage, we will write a user program and store in blocks 7-8 of XSM disk as INIT program.
INT 10
A user program calls the exit system call to return the control back to the operating system using the INT 10 instruction. It will result in graceful termination of the user program. Since we have only one user program for now, we will write the interrupt handler with only "halt" statement.
halt;
Exception handler
We also load the exception handler routine to the memory. The machine may raise an exception if it encounters any unexpected events like illegal instruction, invalid address, page table entry, etc.
load --exhandler ../spl/progs/haltprog.xsm
The OS Startup Code
The OS Startup code of any operating system is the first piece of OS Code to be executed on boostrap, responsible for loading the rest of OS into the memory, initialise OS data structures and set up the first user program for execution.
In this stage, we will write the OS startup code to load the init program and setup the OS data structures necessary, and then transfer the control to INIT program using IRET instruction.
We use loadi instruction for loading a disk block to a memory page, this will temporarily suspend the execution of XSM machine and then proceeds only after the transfer.
The Page Tables are stored from memory address 29696.
IRET Instruction
1) KERNEL to USER mode 2) Instruction pointer is set to the value at the top of user stack 3) The value of SP is decremented by 1.
Why These Lines Work Together (The IRET Mechanism)
The two lines set up the memory and registers for the IRET (Interrupt Return) instruction, which is used to transfer execution from the privileged (Kernel) mode to the unprivileged (User) mode.
-
The kernel physically writes the starting Virtual IP (0) onto the stack frame located at the base of the stack page (76×512).
-
The kernel sets the Virtual Stack Pointer (SP) to the virtual address that corresponds to that physical location (2×512).
-
When the
IRETinstruction is executed, the XSM hardware performs the following actions:-
It fetches the value from the memory location currently pointed to by the Virtual SP (2 * 512), translates this to the physical address 76×512, reads the value 0.
-
It loads this value (0) into the IP (Instruction Pointer) register.
-
It decrements the SP.
-
It changes the CPU mode from Kernel to User.
-
The program begins executing the instruction at Virtual Address 0 with its stack ready at Virtual Address 1024.
Pseudo-code
load INIT
load INT10
load ExceptionHandler
PTBR = PAGE_TABLE_BASE;
PTLR = 10;
// set up the page table of the process
// push first address to the user stack
SP = [User Stack Address]
ireturn;