changeset 408:ad6be5454067

Added image build task
author Windel Bouwman
date Sat, 21 Feb 2015 12:17:47 +0100
parents 9eb1fc6aad6c
children 6aa9743ed362
files build.xml customtasks.py kernel/arch/qemu_vexpress/layout.mmap kernel/arch/qemu_vexpress/start.asm kernel/arch/qemu_vexpress/vexpressA9.c3 kernel/build.xml kernel/kernel.c3 kernel/memory.c3 kernel/process.c3 kernel/ramdisk.asm kernel/schedule.c3 kernel/syscall.c3 run.sh
diffstat 13 files changed, 137 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/build.xml	Fri Feb 20 15:47:54 2015 +0100
+++ b/build.xml	Sat Feb 21 12:17:47 2015 +0100
@@ -1,9 +1,11 @@
 
-<project name="lcfos" default="all">
-    <target name="all">
+<project name="lcfos" default="image">
+    <import name="ppci.buildtasks" />
+    <import name="customtasks" />
+    <target name="image">
         <build file="user/build.xml" />
         <build file="kernel/build.xml" />
+        <mkimage />
     </target>
-
 </project>
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/customtasks.py	Sat Feb 21 12:17:47 2015 +0100
@@ -0,0 +1,33 @@
+"""
+    Custom task to create boot image.
+"""
+from ppci import tasks
+import struct
+
+
+@tasks.register_task('mkimage')
+class MkImageTask(tasks.Task):
+    """ Concatenate kernel and boot data in cunning manner """
+    def run(self):
+        self.logger.info('Constructing boot image')
+        data = bytearray()
+        # TODO: parameterize:
+
+        # Read kernel:
+        with open(self.relpath('kernel/obj/kernel.bin'), 'rb') as f:
+            data += f.read()
+
+        # Read images:
+        with open(self.relpath('user/obj/init.bin'), 'rb') as f:
+            init = f.read()
+
+        # Construct table:
+        table = struct.pack('<II', 0x1337, 1)
+        data += table
+        data += struct.pack('<I', len(init))
+        data += init
+
+
+        # Produce output:
+        with open(self.relpath('image.img'), 'wb') as f:
+            f.write(data)
--- a/kernel/arch/qemu_vexpress/layout.mmap	Fri Feb 20 15:47:54 2015 +0100
+++ b/kernel/arch/qemu_vexpress/layout.mmap	Sat Feb 21 12:17:47 2015 +0100
@@ -1,5 +1,6 @@
 
-MEMORY image LOCATION=0x60010000 SIZE=0x10000 {
+MEMORY image LOCATION=0x60010000 SIZE=0x10000
+{
     SECTION(reset)
     SECTION(code)
     ALIGN(0x4000)
@@ -9,6 +10,7 @@
 }
 
 MEMORY ram LOCATION=0x60020000 SIZE=0x10000 {
+    SECTION(stack)
     SECTION(data)
 }
 
--- a/kernel/arch/qemu_vexpress/start.asm	Fri Feb 20 15:47:54 2015 +0100
+++ b/kernel/arch/qemu_vexpress/start.asm	Sat Feb 21 12:17:47 2015 +0100
@@ -2,7 +2,9 @@
 ; This file contains the low level assembly code required for interrupt
 ; handling and virtual memory.
 
+
 section reset
+; The reset vector:
 
 interrupt_vector_table:
 ivt_reset: B start            ; 0x0 reset
@@ -26,9 +28,7 @@
 
 ; Setup TTBR1 (translation table base register)
 
-
-ldr r0, =kernel_table0    ; Load address of label
-; -KERNEL_BASE
+ldr r0, =kernel_table0    ; Load address of table
 mcr p15, 0, r0, c2, c0, 0 ; TTBR0
 mcr p15, 0, r0, c2, c0, 1 ; TTBR1
 
@@ -56,7 +56,8 @@
 str r0, [r1, 0]
 
 ; Setup stack:
-mov sp, 0x30000
+ldr r0, =stack_top
+mov sp, r0
 BL kernel_start  ; Branch to main (this is actually in the interrupt vector)
 local_loop:
 B local_loop
@@ -86,6 +87,10 @@
 mrc p15, 0, r0, c0, c0, 4
 mov pc, lr
 
+arch_get_image_address:
+ldr r0, =ramdisk_start
+mov pc, lr
+
 ; data:
 txtA:
 dcd 65
@@ -105,15 +110,26 @@
 section mem_tables
 
 kernel_table0:
- dcd 0x60000402 ; Identity map first 1 MB
+ dcd 0
  dcd 0x10000402 ; Map to peripheral space 1 MB
  repeat 0x5FE
  dcd 0
  endrepeat
 
- dcd 0x60000402 ; Alias to 0x0
+ dcd 0x60000402 ; Identity map this!
 
  repeat 0x9FF
  dcd 0
  endrepeat
 
+; Create a label to indicate the ramdisk:
+section ramdisk
+ramdisk_start:
+
+; Create stack space:
+section stack
+
+stack_bot:
+ds 0x1000
+stack_top:
+
--- a/kernel/arch/qemu_vexpress/vexpressA9.c3	Fri Feb 20 15:47:54 2015 +0100
+++ b/kernel/arch/qemu_vexpress/vexpressA9.c3	Sat Feb 21 12:17:47 2015 +0100
@@ -26,6 +26,8 @@
     }
 }
 
+// Functions implemented in assembly:
+function byte* get_image_address();
 function int pfr0();
 function int pfr1();
 function int mmfr0();
--- a/kernel/build.xml	Fri Feb 20 15:47:54 2015 +0100
+++ b/kernel/build.xml	Sat Feb 21 12:17:47 2015 +0100
@@ -4,20 +4,14 @@
     <property name="src" value="src" />
     <property name="arch" value="arm" />
 
-
     <target name="vexpress">
 
         <assemble source="arch/qemu_vexpress/start.asm"
             target="arm" output="obj/start.o" />
 
-        <assemble source="ramdisk.asm"
-            target="arm" output="obj/ramdisk.o" />
-
         <compile target="arm" sources='*.c3;arch/qemu_vexpress/vexpressA9.c3'
             output="obj/kernel.o" />
 
-        <!-- <script file="gen_table.py" /> -->
-
         <link output="obj/kernel.elf"
             target="arm"
             layout="arch/qemu_vexpress/layout.mmap"
@@ -27,7 +21,7 @@
             objectfile="obj/kernel.elf"
             imagename="image"
             format="bin"
-            output="kernel_arm.bin" />
+            output="obj/kernel.bin" />
 
     </target>
 
--- a/kernel/kernel.c3	Fri Feb 20 15:47:54 2015 +0100
+++ b/kernel/kernel.c3	Sat Feb 21 12:17:47 2015 +0100
@@ -2,7 +2,6 @@
 
 import memory;
 import process;
-import scheduler;
 import arch;
 import io;
 import debug;
@@ -13,13 +12,11 @@
 {
     io.println("Welcome to lcfos!");
     arch.init();
-
     memory.init();
     process.init();
     load_init_process();
-
-    io.println("Kernel finished");
-    panic();
+    io.println("Kernel loading finished, now switching to next process");
+    process.execute_next();
 }
 
 // Called in total stress:
@@ -29,12 +26,45 @@
     arch.halt();
 }
 
+type struct {
+    int magic;
+    int num_images;
+} ramdisk_header_t;
+
+// Load init process (first image) from ram image:
 function void load_init_process()
 {
-    var process.process_t* init_proc;
-    init_proc = process.create();
-    process.enqueue(init_proc);
+    // Load image:
+    var byte* image_addr;
+    image_addr = arch.get_image_address();
+    io.print2("ramdisk address: ", cast<int>(image_addr));
 
-    io.print2("init address ", cast<int>(init_proc));
+    var ramdisk_header_t* ramdisk_header;
+    ramdisk_header = image_addr;
+    var byte* image_ptr;
+    image_ptr = image_addr;
+    if (0x1337 == ramdisk_header->magic)
+    {
+        image_ptr += 8;
+        io.print2("Number of images: ", ramdisk_header->num_images);
+        if (ramdisk_header->num_images > 0)
+        {
+            io.println("Loading init");
+            var int init_size;
+            init_size = *(cast<int*>(image_ptr));
+            io.print2("Init size:", init_size);
+            image_ptr += 4;
+
+            // Allocate physical memory:
+            var byte* prog_mem;
+            prog_mem = memory.alloc(init_size);
+            io.print2("Image_ptr:", cast<int>(image_ptr));
+            memory.memcpy(prog_mem, image_ptr, init_size);
+
+            var process.process_t* init_proc;
+            init_proc = process.create();
+            // process.enqueue(init_proc);
+        }
+    }
 }
 
--- a/kernel/memory.c3	Fri Feb 20 15:47:54 2015 +0100
+++ b/kernel/memory.c3	Sat Feb 21 12:17:47 2015 +0100
@@ -9,7 +9,7 @@
 
 function void init()
 {
-    ptr = 0x80000;
+    ptr = 0x60080000;
 }
 
 function byte* alloc(int size)
@@ -25,10 +25,24 @@
     return ptr2;
 }
 
+function byte* allocate_physical()
+{
+    return 0;
+}
+
+function byte* allocate_virtual(byte* address)
+{
+    var byte* address2;
+    address2 = allocate_physical();
+    return address2;
+}
+
 function void memcpy(byte* dst, byte* src, int size)
 {
     var int i;
-    for (i = 0; i < size; i = i + 1)
+    io.print2("memcpy to ", cast<int>(dst));
+    io.print2("memcpy from ", cast<int>(src));
+    for (i = 0; i < size; i += 1)
     {
         *(dst + i) = *(src + i);
     }
--- a/kernel/process.c3	Fri Feb 20 15:47:54 2015 +0100
+++ b/kernel/process.c3	Sat Feb 21 12:17:47 2015 +0100
@@ -14,6 +14,7 @@
 var process_t* first_process;
 var process_t* last_process;
 var int next_pid;
+var process_t* current;
 
 function void init()
 {
@@ -69,3 +70,17 @@
     return 0;
 }
 
+
+function void execute_next()
+{
+    var process.process_t *old;
+
+    old = 0;
+
+    if (old != current)
+    {
+        //execute(current);
+    }
+
+    kernel.panic();
+}
--- a/kernel/ramdisk.asm	Fri Feb 20 15:47:54 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,5 +0,0 @@
-
-section ramdisk
-
-ramdisk_begin:
-
--- a/kernel/schedule.c3	Fri Feb 20 15:47:54 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,20 +0,0 @@
-
-module scheduler;
-
-import process;
-import arch;
-
-var process.process_t *current;
-
-function void execute_next()
-{
-    var process.process_t *old;
-
-    old = 0;
-
-    if (old != current)
-    {
-        //execute(current);
-    }
-}
-
--- a/kernel/syscall.c3	Fri Feb 20 15:47:54 2015 +0100
+++ b/kernel/syscall.c3	Sat Feb 21 12:17:47 2015 +0100
@@ -5,7 +5,6 @@
 */
 
 import arch;
-import scheduler;
 import process;
 
 
--- a/run.sh	Fri Feb 20 15:47:54 2015 +0100
+++ b/run.sh	Sat Feb 21 12:17:47 2015 +0100
@@ -3,6 +3,6 @@
 set -e
 
 # -S means halt at start:
-qemu-system-arm -M vexpress-a9 -m 128M -kernel kernel/kernel_arm.bin \
+qemu-system-arm -M vexpress-a9 -m 128M -kernel image.img \
     -serial stdio