[freenos] r408 committed - Implemented system call support....

  • From: freenos@xxxxxxxxxxxxxx
  • To: freenos@xxxxxxxxxxxxx
  • Date: Tue, 19 Oct 2010 21:40:46 +0000

Revision: 408
Author: nieklinnenbank
Date: Tue Oct 19 14:39:53 2010
Log: Implemented system call support.
Unprivileged user programs may now use any of the
functions defined in FreeNOS/api.h. This initial
version has three exported system calls: retrieving
the current PID, and reading and writing a byte
to an I/O port (no security checks yet).

http://code.google.com/p/freenos/source/detail?r=408

Added:
 /branches/scratch/include/kernel/api.h
 /branches/scratch/include/x86/api.h
 /branches/scratch/system/api.c
 /branches/scratch/system/x86/pc/api.c
Modified:
 /branches/scratch/include/kernel/types.h
 /branches/scratch/include/x86/idt.h
 /branches/scratch/include/x86/irq_table.h
 /branches/scratch/system/x86/pc/idt.c
 /branches/scratch/system/x86/pc/irq.c
 /branches/scratch/system/x86/pc/irq_table.S

=======================================
--- /dev/null
+++ /branches/scratch/include/kernel/api.h      Tue Oct 19 14:39:53 2010
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2010 Niek Linnenbank
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __API_H
+#define __API_H
+
+#include "types.h"
+
+#define API_PROC_ID  (1 << 0)
+#define API_IO_READ  (1 << 1)
+#define API_IO_WRITE (1 << 2)
+
+__inline__ static pid_t api_proc_id(void)
+{
+    return (pid_t) sys_call0(API_PROC_ID);
+}
+
+__inline__ static result_t api_io_read(uint port, size_t size)
+{
+    return sys_call2(API_IO_READ, port, size);
+}
+
+__inline__ static result_t api_io_write(uint port, uint value, size_t size)
+{
+    return sys_call3(API_IO_WRITE, port, value, size);
+}
+
+extern result_t api_execute(uint func, ulong arg1,
+                           ulong arg2, ulong arg3);
+
+#endif /* __API_H */
=======================================
--- /dev/null
+++ /branches/scratch/include/x86/api.h Tue Oct 19 14:39:53 2010
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2010 Niek Linnenbank
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __X86_API_H
+#define __X86_API_H
+
+#include "types.h"
+
+/**
+ * Perform a kernel trap with no arguments.
+ * @param num Unique number of the handler to execute.
+ * @return An integer.
+ */
+__inline__ static ulong sys_call0(ulong num)
+{
+    ulong ret;
+    __asm__ volatile ("int $0x90" : "=a"(ret) : "a"(num));
+    return ret;
+}
+
+/**
+ * Perform a kernel trap with 1 argument.
+ * @param num Unique number of the handler to execute.
+ * @param arg1 First argument becomes ECX.
+ * @param arg2 Second argument becomes EBX.
+ * @return An integer.
+ */
+__inline__ static ulong sys_call1(ulong num, ulong arg1)
+{
+    ulong ret;
+    __asm__ volatile ("int $0x90" : "=a"(ret) : "a"(num), "c"(arg1));
+    return ret;
+}
+
+/**
+ * Perform a kernel trap with 2 arguments.
+ * @param num Unique number of the handler to execute.
+ * @param arg1 First argument becomes ECX.
+ * @param arg2 Second argument becomes EBX.
+ * @return An integer.
+ */
+__inline__ static ulong sys_call2(ulong num, ulong arg1, ulong arg2)
+{
+    ulong ret;
+    __asm__ volatile ("int $0x90" : "=a"(ret) : "a"(num), "c"(arg1),
+                                               "b"(arg2));
+    return ret;
+}
+
+/**
+ * Perform a kernel trap with 3 arguments.
+ * @param num Unique number of the handler to execute.
+ * @param arg1 First argument becomes ECX.
+ * @param arg2 Second argument becomes EBX.
+ * @param arg3 Third argument becomes EDX.
+ * @return An integer..
+ */
+__inline__ static ulong sys_call3(ulong num, ulong arg1,
+                                 ulong arg2, ulong arg3)
+{
+    ulong ret;
+    __asm__ volatile ("int $0x90" : "=a"(ret) : "a"(num), "c"(arg1),
+                                               "b"(arg2), "d"(arg3));
+    return ret;
+}
+
+#include <kernel/api.h>
+
+#endif /* __X86_API_H */
=======================================
--- /dev/null
+++ /branches/scratch/system/api.c      Tue Oct 19 14:39:53 2010
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2010 Niek Linnenbank
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define API_PROC_ID  (1 << 0)
+#define API_IO_READ  (1 << 1)
+#define API_IO_WRITE (1 << 2)
+
+#include <FreeNOS/api.h>
+#include <FreeNOS/io.h>
+#include <FreeNOS/types.h>
+#include <FreeNOS/process.h>
+#include <FreeNOS/printk.h>
+#include <FreeNOS/error.h>
+#include <FreeNOS/smp.h>
+
+result_t api_execute(uint func,  ulong arg1,
+                    ulong arg2, ulong arg3)
+{
+    switch (func)
+    {
+       case API_PROC_ID:
+           return proc_lookup(smp_cpu()->proc);
+
+       case API_IO_READ:
+           return inb(arg1);
+
+       case API_IO_WRITE:
+           outb(arg1, arg2);
+           return 0;
+
+       default:
+           break;
+    }
+    return -EINVAL;
+}
=======================================
--- /dev/null
+++ /branches/scratch/system/x86/pc/api.c       Tue Oct 19 14:39:53 2010
@@ -0,0 +1,1 @@
+link ../../api.c
=======================================
--- /branches/scratch/include/kernel/types.h    Sun Oct 10 16:06:55 2010
+++ /branches/scratch/include/kernel/types.h    Tue Oct 19 14:39:53 2010
@@ -99,4 +99,3 @@

 #endif /* __ASSEMBLER__ */
 #endif /* __TYPES_H */
-
=======================================
--- /branches/scratch/include/x86/idt.h Mon Oct 18 16:48:04 2010
+++ /branches/scratch/include/x86/idt.h Tue Oct 19 14:39:53 2010
@@ -24,6 +24,7 @@
 #define IDT_NUM        256
 #define IDT_TRAP_GATE 0x8f
 #define IDT_INT_GATE  0x8e
+#define IDT_SYS_GATE  0xee

 typedef struct idt_entry
 {
=======================================
--- /branches/scratch/include/x86/irq_table.h   Sun Oct 10 16:06:55 2010
+++ /branches/scratch/include/x86/irq_table.h   Tue Oct 19 14:39:53 2010
@@ -69,6 +69,7 @@
 extern void i46(void);
 extern void i47(void);
 extern void i48(void);
+extern void i144(void);

 #endif /* __ASSEMBLER__ */
 #endif /* __IRQ_TABLE_H */
=======================================
--- /branches/scratch/system/x86/pc/idt.c       Mon Oct 18 16:48:04 2010
+++ /branches/scratch/system/x86/pc/idt.c       Tue Oct 19 14:39:53 2010
@@ -65,6 +65,7 @@
     idt_set_entry(46, IDT_INT_GATE,  (addr_t) i46);
     idt_set_entry(47, IDT_INT_GATE,  (addr_t) i47);
     idt_set_entry(48, IDT_INT_GATE,  (addr_t) i48);
+    idt_set_entry(0x90, IDT_SYS_GATE, (addr_t) i144);
 }

 void idt_load(void)
=======================================
--- /branches/scratch/system/x86/pc/irq.c       Sun Oct 10 16:06:55 2010
+++ /branches/scratch/system/x86/pc/irq.c       Tue Oct 19 14:39:53 2010
@@ -19,6 +19,7 @@
 #include <FreeNOS/idt.h>
 #include <FreeNOS/pic.h>
 #include <FreeNOS/smp.h>
+#include <FreeNOS/api.h>
 #include <FreeNOS/printk.h>

 static const char *exceptions[] = {
@@ -76,6 +77,12 @@
            state->eip, state->eflags,
            state->cs, state->ss0, state->ds, state->es, state->fs, state->gs);
 }
+
+void trap(cpu_state_t *state)
+{
+    state->eax = api_execute(state->eax, state->ecx,
+                            state->ebx, state->edx);
+}

 void irq_init(void)
 {
@@ -86,6 +93,8 @@
     {
        if (i <= 15)
            irq_handler[i] = exception;
+       else if (i == 0x90)
+           irq_handler[i] = trap;
        else
            irq_handler[i] = irq_ignore;
     }
=======================================
--- /branches/scratch/system/x86/pc/irq_table.S Mon Oct 18 17:03:00 2010
+++ /branches/scratch/system/x86/pc/irq_table.S Tue Oct 19 14:39:53 2010
@@ -24,7 +24,7 @@
 IRQ(15)   IRQ(16)   IRQ(32)   IRQ(33)   IRQ(34)
 IRQ(35)   IRQ(36)   IRQ(37)   IRQ(38)   IRQ(39)
 IRQ(40)   IRQ(41)   IRQ(42)   IRQ(43)   IRQ(44)
-IRQ(45)   IRQ(46)   IRQ(47)   IRQ(48)
+IRQ(45)   IRQ(46)   IRQ(47)   IRQ(48)  IRQ(144)

 irq_execute:


Other related posts:

  • » [freenos] r408 committed - Implemented system call support.... - freenos