Home

root/syscalls.s

/* [<][>][^][v][top][bottom][index][help] */
    SLEEPINT = 2

    .include "include/memory.inc"
    .include "include/kstructs.inc"
    .include "macros.s"

    .text

    .global SysCalls
    .global GoToSleep
    .global SetCurrentDirectory

CallNo:
    .quad   Sys_Exit
    .quad   Sys_Fork
    .quad   Sys_Read
    .quad   Sys_Write
    .quad   Sys_Open
    .quad   Sys_Close   
    .quad   SysWaitPID
    .quad   Sys_Creat
    .quad   Unimplemented   #Sys_Link
    .quad   Sys_UnLink
    .quad   Sys_Execve
    .quad   Sys_ChDir
    .quad   Unimplemented   #Sys_Time
    .quad   Unimplemented   #Sys_MkNod
    .quad   Unimplemented   #Sys_ChMod
    .quad   Unimplemented   #Sys_LChOwn
    .quad   Unimplemented   #Unused
    .quad   Sys_Stat
    .quad   Sys_LSeek
    .quad   Unimplemented   #Sys_GetPid
    .quad   Unimplemented   #Sys_Mount
    .quad   Unimplemented   #Sys_Umount
    .quad   Unimplemented   #Sys_SetUID
    .quad   Unimplemented   #Sys_GetUID
    .quad   Unimplemented   #Sys_Stime
    .quad   Unimplemented   #Sys_Ptrace
    .quad   Unimplemented   #Sys_Alarm
    .quad   Sys_FStat
    .quad   GetTicks
    .quad   Sys_Nanosleep
    .quad   Alloc_Mem
    .quad   Send_Message
    .quad   Receive_Message
    .quad   Dealloc_Mem
    .quad   Send_Receive
    .quad   GetCurrentConsole
    .quad   Sys_Getcwd
    .quad   Sys_MkDir

SysCalls:
    jmp *(CallNo - 8)(,%r9, 8)  

#=========================================================
# Kill the current task, freeing all memory owned by task
# This syscall should never return
#=========================================================
Sys_Exit:
    push %rcx
    call KillTask
    pop  %rcx
    sysretq

#=========================================================
# Creates an exact copy of the current task
# Returns in RAX either the pid of the new file or 0
# when returning to the new file. Note that this function
# returns both in the calling program and the copy.
#=========================================================  
Sys_Fork:
    push %rcx
    call DoFork
    pop %rcx
    sysretq
    
#=========================================================
# Reads from the file whose FCB is in RDI
# RSI = buffer to read into
# RDX = number of bytes to read
# Returns actual number read in RAX
#=========================================================  
Sys_Read:
    push %rcx
    call DoRead
    pop %rcx
    sysretq
    
#=========================================================
# Writes to the file whose FCB is in RDI
# RSI = buffer to write from
# RDX = number of bytes to write
# Returns actual number written in RAX
#=========================================================
Sys_Write:
    push %rcx
    call DoWrite
    pop %rcx
    sysretq
    
#=========================================================
# Opens the file whose name is pointed to by RDI.
# Returns in RAX the File Descriptor
#=========================================================
Sys_Open:
    push %rcx
    call KOpenFile
    pop %rcx
    sysretq
    
#=========================================================
# Closes the file whose FD is pointed to by RDI
#=========================================================
Sys_Close:
    push %rcx
    call KCloseFile
    pop %rcx
    sysretq             

#=========================================================
# Tells this process to wait for the task with pid RDI
# to finish. The task will be sent a message when this
# happens
#=========================================================
SysWaitPID:
    push %rcx
    call Do_Wait
    pop %rcx
    sysretq
    
#=========================================================
# Create a new file whose name is pointed to by RDI
#=========================================================
Sys_Creat:
    push %rcx
    call DoCreate
    pop %rcx
    sysretq

#=========================================================
# Unimplemented
#=========================================================
Unimplemented:   #Sys_Link:
    sysretq

#=========================================================
# Delete the file whose name is pointed to by RDI
#=========================================================
Sys_UnLink:
    push %rcx
    call DoDelete
    pop %rcx
    sysretq

#=========================================================
# Replace the current task code with that in the file
# pointed to by RAX
#=========================================================
Sys_Execve:
    push %rcx
    call DoExec
    cmp $1,%rax
    je  notLoaded
    mov $(UserStack + PageSize), %rsp
    push $UserCode
notLoaded:
    pop %rcx
    sysretq

#=========================================================
# Changes to the directory whose name is pointed to by RDI
#=========================================================
Sys_ChDir:
    push %rcx
    call DoChDir
    pop %rcx
    sysretq             

#=========================================================
# Returns information about the file whose name is pointed
# to by RDI to the buffer pointed to by RSI
#=========================================================

Sys_Stat:
    push %rcx
    call DoStat
    pop %rcx
    sysretq

#=========================================================
# Returns information about the file whose FCB is in RDI
# to the buffer pointed to by RSI
#=========================================================
Sys_FStat:
    push %rcx
    call DoFStat
    pop %rcx
    sysretq

#=========================================================
# Changes the buffer cursor in the FCB RDI to the offset
# in RSI. RDX determines where the offset is from.
#=========================================================
Sys_LSeek:
    push %rcx
    call Do_Seek
    pop %rcx
    sysretq


#========================================================
# Print [EDX] as string at position row BH col BL
# Affects RAX, RBX, RDX
#========================================================
PrintString:
    mov $160, %ax
    mul %bh
    mov $0, %bh
    shl $1, %bx
    add %ax, %bx
.isItStringEnd:
    mov (%edx), %ah
    cmp $0, %ah
    je .done
    mov %ah, 0xB8000(%ebx)
    add $2, %bx
    inc %edx
    jmp .isItStringEnd
.done:  sysretq

#========================================================
# Print EDX as hex at position row BH col BL
# Affects RAX, RBX, RDX
#========================================================
PrintDouble:
    push %rcx
    mov $160, %ax
    mul %bh
    mov $0, %bh
    shl $1, %bx
    add %ax, %bx
    mov $8, %rcx
.stillCounting:
    shld $4, %edx, %eax
    shl $4, %edx
    and $0xF, %eax
    add $'0, %al
    cmp $'9, %al
    jle .under10
    add $7, %al
.under10:
    mov  %al, 0xB8000(%ebx)
    add  $2, %bx
    loop .stillCounting
    pop  %rcx
    sysretq

#========================================================
# Print character in AH at position row BH col BL
# Affects RAX, RBX
#========================================================
PrintChar:
    push %ax
    mov  $160, %ax
    mul  %bh
    mov  $0, %bh
    shl  $1, %bx
    add  %ax, %bx
    pop  %ax
    mov  %ah, 0xB8000(%ebx)
    sysretq


#=============================================================================
# Return in RAX the number of (10ms) clock ticks since the system was started
#=============================================================================
GetTicks:
    mov Ticks, %rax
    sysretq

#=====================================================
# Suspend the current task for for RDI 1ms intervals
#=====================================================
Sys_Nanosleep:  
    push %rcx
    mov currentTask, %r15
    movb $SLEEPINT, TS.waiting(%r15)
    mov %rdi, TS.timer(%r15)
    mov %r15, %rdi
    call BlockTask
    SWITCH_TASKS               # The current task is no longer runnable
    pop %rcx
    sysretq

#==============================================================
# Allocate some memory from the heap. RDI = amount to allocate
# Returns in RAX address of allocated memory.
#==============================================================
Alloc_Mem:
    push %rcx
    mov  currentTask, %r15
    mov  TS.firstfreemem(%r15), %rsi
    call AllocMem
    pop  %rcx
    sysretq

#===================================
# Send a message to a message port.
# RDI = message port
# RSI = message
#===================================
Send_Message:
    push %rcx
    call SendMessage
    pop %rcx
    sysretq

#===========================================================
# Receive a message on message port RDI
# RSI = Buffer to return message to
# If there is no message on the port block and wait for one
#===========================================================
Receive_Message:
    push %rcx
    call ReceiveMessage
    pop %rcx
    sysretq

#==================================================
# Deallocate the memory at location RDI.
# This will deallocate both user and kernel memory
#==================================================
Dealloc_Mem:
    push %rcx
    call DeallocMem
    pop %rcx
    sysretq

#======================================================
# Send a message to a message port and receive a reply
# RDI = message port
# RSI = message
#======================================================
Send_Receive:
    push %rcx
    call SendReceiveMessage
    pop %rcx
    sysretq

#=================================================
# Returns the console (0 - 3) of the current task
#=================================================
GetCurrentConsole:
    mov currentTask, %r15
    mov TS.console(%r15), %rax
    sysretq

#===================================================
# Returns the current directory of the current task
#===================================================
Sys_Getcwd:
    push %rcx
    call DoGetcwd
    pop %rcx
    sysretq

#========================================================
# Creates a directory
# RSI = name
#========================================================
Sys_MkDir:
    push %rcx
    call DoMkDir
    pop %rcx
    sysretq
    
#========================================================
# Tell the current task to sleep for RDI nanoseconds
#========================================================
GoToSleep:
    push %rdx
    mov currentTask, %r15
    movb $SLEEPINT, TS.waiting(%r15)
    mov %rdi, TS.timer(%r15)
    mov %r15, %rdi
    call BlockTask
    pop %rdx
    SWITCH_TASKS               # The current task is no longer runnable
    ret


/* [<][>][^][v][top][bottom][index][help] */
Home