From 7ecdc87d89c7321d78df7ed03f97c0d0712ee733 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Sun, 5 Jun 2011 21:39:57 +0200 Subject: [PATCH 1/4] integration of MetalSVM features into LwIP - setting LwIP macro NO_SYS to 0 - this approach based on Carl-Benedikt Krueger's LwIP branch "krueger" --- lwip/src/arch/sys_arch.c | 216 ++++++++++++++++++++++++++++++- lwip/src/include/arch/sys_arch.h | 42 +++--- lwip/src/include/lwipopts.h | 30 ++++- 3 files changed, 262 insertions(+), 26 deletions(-) diff --git a/lwip/src/arch/sys_arch.c b/lwip/src/arch/sys_arch.c index 185cc282..ca3b15d4 100644 --- a/lwip/src/arch/sys_arch.c +++ b/lwip/src/arch/sys_arch.c @@ -24,9 +24,13 @@ #include "lwip/opt.h" #include "lwip/stats.h" -void sys_init(void) -{ -} +#ifndef TRUE +#define TRUE 1 +#endif + +#ifndef FALSE +#define FALSE 0 +#endif /** Returns the current time in milliseconds, * may be the same as sys_jiffies or at least based on it. */ @@ -41,3 +45,209 @@ sys_jiffies(void) { return (get_clock_tick() / TIMER_FREQ) * 1000; } + +#if !NO_SYS + +/* sys_init(): init needed system resources + * Note: At the moment there are none + */ +void +sys_init(void) +{ +} + +/** + * Sleep for some ms. Timeouts are NOT processed while sleeping. + * + * @param ms number of milliseconds to sleep + */ +void +sys_msleep(u32_t ms) +{ + if (ms > 0) { + sys_sem_t delaysem; + err_t err = sys_sem_new(&delaysem, 0); + if (err == ERR_OK) { + sys_arch_sem_wait(&delaysem, ms); + sys_sem_free(&delaysem); + } + } +} + +/* sys_thread_new(): Spawns a new thread with given attributes as supportet + * Note: In MetalSVM this is realized as kernel tasks + */ +sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio) +{ + tid_t tmp; + create_kernel_task(&tmp,thread,arg); + return tmp; +} + +/* sys_sem_free(): destroy's given semaphore + * and releases system resources. + * This semaphore also gets invalid. + */ +void sys_sem_free(sys_sem_t* sem) +{ + sem->valid = FALSE; + sem_destroy(&sem->sem); +} + +/* sys_sem_valid(): returns if semaphore is valid + * at the moment + */ +int sys_sem_valid(sys_sem_t* sem) +{ + return sem->valid; +} + +/* sys_sem_new(): creates a new semaphre with given count. + * This semaphore becomes valid + */ +err_t sys_sem_new(sys_sem_t* sem,u8_t count) +{ + sem->valid = TRUE; + return sem_init(&sem->sem,count); +} + +/* sys_sem_set_invalid(): this semapohore becomes invalid + * Note: this does not mean it is destroyed + */ +void sys_sem_set_invalid(sys_sem_t * sem) +{ + sem->valid = FALSE; +} + +/* sys_sem_signal(): this semaphore is signaled + * + */ +void sys_sem_signal(sys_sem_t* sem) +{ + sem_post(&sem->sem); +} + +/* sys_arch_sem_wait): wait for the given semaphore for + * a given timeout + * Note: timeout = 0 means wait forever + */ +u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout) +{ + int err; + if (!timeout) + return sem_wait(&sem->sem); + while (timeout) + { + err = sem_trywait(&sem->sem); + if (err != -1) + return err; + timeout--; + } + return SYS_ARCH_TIMEOUT; +} + +/* sys_mbox_valid() : returns if the given mailbox + * is valid + */ +int sys_mbox_valid(sys_mbox_t * mbox) +{ + return mbox->valid; +} + +/* sys_arch_mbox_fetch(): wait for the given mailbox for a specified + * amount of time. + * Note: timeout = 0 means wait forever + */ +u32_t sys_arch_mbox_fetch(sys_mbox_t * mbox, void **msg, u32_t timeout) +{ + if (!timeout) + return mailbox_ptr_fetch(&mbox->mailbox,msg); + + while(timeout) + { + if (!mailbox_ptr_tryfetch(&mbox->mailbox,msg)) + return 0; + timeout--; + } + + return SYS_ARCH_TIMEOUT; +} + +/* sys_mbox_free() : free the given mailbox, release the system resources + * and set mbox to invalid + */ +void sys_mbox_free(sys_mbox_t* mbox) +{ + mbox->valid = FALSE; + mailbox_ptr_destroy(&mbox->mailbox); +} + +/* sys_arch_mbox_tryfetch(): poll for new data in mailbox + * + */ +u32_t sys_arch_mbox_tryfetch(sys_mbox_t* mbox, void** msg) +{ + return mailbox_ptr_tryfetch(&mbox->mailbox,msg); +} + +/* sys_mbox_new(): create a new mailbox with a minimum size of "size" + * + */ +err_t sys_mbox_new(sys_mbox_t* mbox,int size) +{ + mbox->valid = TRUE; + return mailbox_ptr_init(&mbox->mailbox); +} + +/* sys_mbox_set_invalid(): set the given mailbox to invald + * Note: system resources are NOT freed + */ +void sys_mbox_set_invalid(sys_mbox_t* mbox) +{ + mbox->valid = FALSE; +} + +/* sys_mbox_trypost(): try to post data to the mailbox + * Note: There is at the moment no try post implemented + * so we use the normal post instead + */ +err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg) +{ + return mailbox_ptr_post(&mbox->mailbox,msg); +} + +/* sys_mbox_post(): post new data to the mailbox + * + */ +void sys_mbox_post(sys_mbox_t* mbox,void* msg) +{ + mailbox_ptr_post(&mbox->mailbox,msg); +} + +/* sys_mutex_lock(): lock the given mutex + * Note: There is no specific mutex in MetalSVM + * so we use a semaphore with 1 element + */ +void sys_mutex_lock(sys_mutex_t* mutex) +{ + sem_wait(mutex); +} + +/* sys_mutex_unlock(): unlock the given mutex + * + */ +void sys_mutex_unlock(sys_mutex_t* mutex) +{ + sem_post(mutex); +} + +/* sys_mutex_new(): create a new mutex + * + */ +err_t sys_mutex_new(sys_mutex_t * mutex) +{ + sem_init(mutex,1); + return 0; +} + +#endif /* !NO_SYS */ diff --git a/lwip/src/include/arch/sys_arch.h b/lwip/src/include/arch/sys_arch.h index 6074d05a..d55cde38 100644 --- a/lwip/src/include/arch/sys_arch.h +++ b/lwip/src/include/arch/sys_arch.h @@ -1,20 +1,22 @@ -/* - * Copyright (c) 2010 Stefan Lankes, Chair for Operating Systems, RWTH Aachen University, Germany - * All rights reserved - */ -#ifndef __ARCH_SYS_ARCH_H__ -#define __ARCH_SYS_ARCH_H__ - -#include -#include -#include - -#define SYS_MBOX_NULL NULL -#define SYS_SEM_NULL NULL - -typedef sem_t* sys_sem_t; -typedef mailbox_ptr_t* sys_mbox_t; -typedef tid_t* sys_thread_t; - -#endif /* __ARCH_SYS_ARCH_H__ */ - +#ifndef __ARCH_SYS_ARCH_H__ +#define __ARCH_SYS_ARCH_H__ + +#include +#include + +typedef sem_t sys_mutex_t; + +typedef struct +{ + sem_t sem; + int valid; +} sys_sem_t; + +typedef struct +{ mailbox_ptr_t mailbox; + int valid; +} sys_mbox_t; + +typedef tid_t* sys_thread_t; + +#endif /* __ARCH_SYS_ARCH_H__ */ diff --git a/lwip/src/include/lwipopts.h b/lwip/src/include/lwipopts.h index 7279f5c7..f96a41f1 100644 --- a/lwip/src/include/lwipopts.h +++ b/lwip/src/include/lwipopts.h @@ -7,7 +7,7 @@ * NO_SYS==1: Provides VERY minimal functionality. Otherwise, * use lwIP facilities. */ -#define NO_SYS 1 +#define NO_SYS 0 /** * NO_SYS_NO_TIMERS==1: Drop support for sys_timeout when NO_SYS==1 @@ -40,6 +40,30 @@ */ #define LWIP_TCP 1 +/** + * TCP_QUEUE_OOSEQ==1: TCP will queue segments that arrive out of order. + * Define to 0 if your device is low on memory. + */ +#ifndef TCP_QUEUE_OOSEQ +#define TCP_QUEUE_OOSEQ 0 +#endif + +/** + * TCP_SND_BUF: TCP sender buffer space (bytes). + */ +#ifndef TCP_SND_BUF +#define TCP_SND_BUF 2048 +#endif + +/** + * TCP_SND_QUEUELEN: TCP sender buffer space (pbufs). This must be at least + * as much as (2 * TCP_SND_BUF/TCP_MSS) for things to work. + */ +#ifndef TCP_SND_QUEUELEN +#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1))/(TCP_MSS)) +#endif + + /** * LWIP_BROADCAST_PING==1: respond to broadcast pings (default is unicast only) */ @@ -53,13 +77,13 @@ /** * LWIP_HAVE_LOOPIF==1: Support loop interface (127.0.0.1) and loopif.c */ -#define LWIP_HAVE_LOOPIF 1 +#define LWIP_HAVE_LOOPIF 0 /** * LWIP_NETIF_LOOPBACK==1: Support sending packets with a destination IP * address equal to the netif IP address, looping them back up the stack. */ -#define LWIP_NETIF_LOOPBACK 1 +#define LWIP_NETIF_LOOPBACK 0 /** * MEMP_NUM_SYS_TIMEOUT: the number of simulateously active timeouts. From 3b79af56a3d7a34158096de5f4ce533f7e49a401 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Mon, 6 Jun 2011 21:45:10 +0200 Subject: [PATCH 2/4] add the support of the Socket API --- lwip/src/api/Makefile | 2 +- lwip/src/include/arch/sys_arch.h | 3 +++ lwip/src/include/lwipopts.h | 45 +++++++------------------------- 3 files changed, 14 insertions(+), 36 deletions(-) diff --git a/lwip/src/api/Makefile b/lwip/src/api/Makefile index bb5170be..01a9faf4 100644 --- a/lwip/src/api/Makefile +++ b/lwip/src/api/Makefile @@ -1,4 +1,4 @@ -C_source := err.c +C_source := api_lib.c api_msg.c err.c netbuf.c netifapi.c sockets.c tcpip.c MODULE := lwip_src_api include $(TOPDIR)/Makefile.inc diff --git a/lwip/src/include/arch/sys_arch.h b/lwip/src/include/arch/sys_arch.h index d55cde38..fde4fbc2 100644 --- a/lwip/src/include/arch/sys_arch.h +++ b/lwip/src/include/arch/sys_arch.h @@ -3,6 +3,9 @@ #include #include +#include + +#define EWOULDBLOCK EAGAIN /* Operation would block */ typedef sem_t sys_mutex_t; diff --git a/lwip/src/include/lwipopts.h b/lwip/src/include/lwipopts.h index f96a41f1..1565c8af 100644 --- a/lwip/src/include/lwipopts.h +++ b/lwip/src/include/lwipopts.h @@ -9,21 +9,15 @@ */ #define NO_SYS 0 -/** - * NO_SYS_NO_TIMERS==1: Drop support for sys_timeout when NO_SYS==1 - * Mainly for compatibility to old versions. - */ -#define NO_SYS_NO_TIMERS 0 - /** * LWIP_SOCKET==1: Enable Socket API (require to use sockets.c) */ -#define LWIP_SOCKET 0 +#define LWIP_SOCKET 1 /** * LWIP_NETCONN==1: Enable Netconn API (require to use api_lib.c) */ -#define LWIP_NETCONN 0 +#define LWIP_NETCONN 1 /** * LWIP_DHCP==1: Enable DHCP module. @@ -40,29 +34,10 @@ */ #define LWIP_TCP 1 -/** - * TCP_QUEUE_OOSEQ==1: TCP will queue segments that arrive out of order. - * Define to 0 if your device is low on memory. - */ -#ifndef TCP_QUEUE_OOSEQ -#define TCP_QUEUE_OOSEQ 0 -#endif - /** * TCP_SND_BUF: TCP sender buffer space (bytes). */ -#ifndef TCP_SND_BUF #define TCP_SND_BUF 2048 -#endif - -/** - * TCP_SND_QUEUELEN: TCP sender buffer space (pbufs). This must be at least - * as much as (2 * TCP_SND_BUF/TCP_MSS) for things to work. - */ -#ifndef TCP_SND_QUEUELEN -#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1))/(TCP_MSS)) -#endif - /** * LWIP_BROADCAST_PING==1: respond to broadcast pings (default is unicast only) @@ -74,22 +49,22 @@ */ #define LWIP_MULTICAST_PING 1 +/** + * MEMP_NUM_SYS_TIMEOUT: the number of simulateously active timeouts. + * (requires NO_SYS==0) + */ +#define MEMP_NUM_SYS_TIMEOUT 7 + /** * LWIP_HAVE_LOOPIF==1: Support loop interface (127.0.0.1) and loopif.c */ -#define LWIP_HAVE_LOOPIF 0 +#define LWIP_HAVE_LOOPIF 1 /** * LWIP_NETIF_LOOPBACK==1: Support sending packets with a destination IP * address equal to the netif IP address, looping them back up the stack. */ -#define LWIP_NETIF_LOOPBACK 0 - -/** - * MEMP_NUM_SYS_TIMEOUT: the number of simulateously active timeouts. - * (requires NO_SYS==0) - */ -#define MEMP_NUM_SYS_TIMEOUT 7 +#define LWIP_NETIF_LOOPBACK 1 /** * LWIP_CHECKSUM_ON_COPY==1: Calculate checksum when copying data from From 393808e198a2f6bf32f0af235f13f76e2c08a894 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Thu, 9 Jun 2011 08:18:12 +0200 Subject: [PATCH 3/4] fix bug in the initializtion of the BSS section - DO NOT LONGER USE the compiler flag "-fno-zero-initialized-in-bss" --- Makefile.example | 4 ++-- arch/x86/kernel/entry.asm | 2 +- arch/x86/kernel/gdt.c | 4 ++-- arch/x86/kernel/multiboot.c | 2 +- kernel/init.c | 12 ++++++++++++ link.ld | 5 ++--- 6 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Makefile.example b/Makefile.example index 71ff1725..3e87db1e 100644 --- a/Makefile.example +++ b/Makefile.example @@ -26,9 +26,9 @@ MAKE = make NASMFLAGS = -felf32 -g INCLUDE = -I$(TOPDIR)/include -I$(TOPDIR)/arch/$(ARCH)/include -I$(TOPDIR)/lwip/src/include -I$(TOPDIR)/lwip/src/include/ipv4 -I$(TOPDIR)/drivers # Compiler options for final code -CFLAGS = -g -m32 -march=i586 -Wall -O2 -fno-zero-initialized-in-bss -fno-builtin -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc $(INCLUDE) -fno-stack-protector +CFLAGS = -g -m32 -march=i586 -Wall -O2 -fno-builtin -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc $(INCLUDE) -fno-stack-protector # Compiler options for debuuging -#CFLAGS = -g -O -m32 -march=i586 -Wall -fno-zero-initialized-in-bss -fno-builtin -DWITH_FRAME_POINTER -nostdinc $(INCLUDE) -fno-stack-protector +#CFLAGS = -g -O -m32 -march=i586 -Wall -fno-builtin -DWITH_FRAME_POINTER -nostdinc $(INCLUDE) -fno-stack-protector ARFLAGS = rsv RM = rm -rf LDFLAGS = -T link.ld -z max-page-size=4096 --defsym __BUILD_DATE=$(shell date +'%Y%m%d') --defsym __BUILD_TIME=$(shell date +'%H%M%S') diff --git a/arch/x86/kernel/entry.asm b/arch/x86/kernel/entry.asm index 5610c6b8..6fb00652 100644 --- a/arch/x86/kernel/entry.asm +++ b/arch/x86/kernel/entry.asm @@ -73,7 +73,7 @@ stublet: ; clears the current pgd entry xor eax, eax mov cr3, eax -; disable SSE support (TODO) +; at this stage, we disable the SSE support mov eax, cr4 and eax, 0xfffbf9ff mov cr4, eax diff --git a/arch/x86/kernel/gdt.c b/arch/x86/kernel/gdt.c index 5be409c6..53a83b95 100644 --- a/arch/x86/kernel/gdt.c +++ b/arch/x86/kernel/gdt.c @@ -28,10 +28,10 @@ gdt_ptr_t gp; static tss_t task_state_segments[MAX_TASKS] __attribute__ ((aligned (PAGE_SIZE))); -static unsigned char kstacks[MAX_TASKS][KERNEL_STACK_SIZE] __attribute__ ((aligned (PAGE_SIZE))); +static unsigned char kstacks[MAX_TASKS][KERNEL_STACK_SIZE] __attribute__ ((aligned (PAGE_SIZE) section (".data"))); // currently, our kernel has full access to the ioports static gdt_entry_t gdt[GDT_ENTRIES] = {[0 ... GDT_ENTRIES-1] = {0, 0, 0, 0, 0, 0}}; -unsigned char* default_stack_pointer = kstacks[0] + KERNEL_STACK_SIZE - sizeof(size_t); +unsigned char* default_stack_pointer __attribute__ ((section (".data"))) = kstacks[0] + KERNEL_STACK_SIZE - sizeof(size_t); /* * This is in start.asm. We use this to properly reload diff --git a/arch/x86/kernel/multiboot.c b/arch/x86/kernel/multiboot.c index d57b5f96..ec124479 100644 --- a/arch/x86/kernel/multiboot.c +++ b/arch/x86/kernel/multiboot.c @@ -37,7 +37,7 @@ */ /** Global multiboot information structure pointer */ -multiboot_info_t* mb_info = NULL; +multiboot_info_t* mb_info __attribute__ ((section (".data"))) = NULL; #endif /** @brief initialization procedure for Multiboot information structure diff --git a/kernel/init.c b/kernel/init.c index 2ba3b81f..c4dea9b7 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -43,10 +43,22 @@ void ping_init(void); static volatile int done = 0; +/* + * Note that linker symbols are not variables, they have no memory allocated for + * maintaining a value, rather their address is their value. + */ +extern const void bss_start; +extern const void bss_end; + int lowlevel_init(void) { + // initialize .bss section + memset((void*)&bss_start, 0x00, ((size_t) &bss_end - (size_t) &bss_start)); + koutput_init(); + //kprintf("Now, the BSS section (0x%x - 0x%x) is initialized.\n", (size_t) &bss_start, (size_t) &bss_end); + return 0; } diff --git a/link.ld b/link.ld index bbb2a9d4..ad03b8f5 100644 --- a/link.ld +++ b/link.ld @@ -13,9 +13,6 @@ SECTIONS .text ALIGN(4096) : AT(ADDR(.text)) { *(.text) } - .rdata ALIGN(4096) : AT(ADDR(.rdata)) { - *(.rdata) - } .rodata ALIGN(4096) : AT(ADDR(.rodata)) { *(.rodata) *(.rodata.*) @@ -23,8 +20,10 @@ SECTIONS .data ALIGN(4096) : AT(ADDR(.data)) { *(.data) } + bss_start = .; .bss ALIGN(4096) : AT(ADDR(.bss)) { *(.bss) } + bss_end = .; kernel_end = .; } From 9085bb67e48bdad899759b95074231207d6adb24 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Thu, 9 Jun 2011 08:29:06 +0200 Subject: [PATCH 4/4] fix typo --- arch/x86/kernel/gdt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kernel/gdt.c b/arch/x86/kernel/gdt.c index 53a83b95..ed863eaa 100644 --- a/arch/x86/kernel/gdt.c +++ b/arch/x86/kernel/gdt.c @@ -28,7 +28,7 @@ gdt_ptr_t gp; static tss_t task_state_segments[MAX_TASKS] __attribute__ ((aligned (PAGE_SIZE))); -static unsigned char kstacks[MAX_TASKS][KERNEL_STACK_SIZE] __attribute__ ((aligned (PAGE_SIZE) section (".data"))); +static unsigned char kstacks[MAX_TASKS][KERNEL_STACK_SIZE] __attribute__ ((aligned (PAGE_SIZE), section (".data"))); // currently, our kernel has full access to the ioports static gdt_entry_t gdt[GDT_ENTRIES] = {[0 ... GDT_ENTRIES-1] = {0, 0, 0, 0, 0, 0}}; unsigned char* default_stack_pointer __attribute__ ((section (".data"))) = kstacks[0] + KERNEL_STACK_SIZE - sizeof(size_t);