diff --git a/main.cpp b/main.cpp index 1db1b55a3400dcc2265955135b0f46dfb5631557..257b9034f21b87738e3f77de12227271981ce15a 100644 --- a/main.cpp +++ b/main.cpp @@ -8,7 +8,7 @@ using namespace std; using namespace miosix; int main() -{ +{ getchar(); ElfProgram prog(reinterpret_cast<const unsigned int*>(main_elf),main_elf_len); diff --git a/miosix/arch/cortexM3_stm32f2/common/CMSIS/system_stm32f2xx.c b/miosix/arch/cortexM3_stm32f2/common/CMSIS/system_stm32f2xx.c index 7094d50524256331888a08cc8e282fc57d5d0e5b..61e50cb7de686e42d30e7ccb859751267b19af05 100644 --- a/miosix/arch/cortexM3_stm32f2/common/CMSIS/system_stm32f2xx.c +++ b/miosix/arch/cortexM3_stm32f2/common/CMSIS/system_stm32f2xx.c @@ -443,23 +443,29 @@ void SystemInit_ExtMemCtl(void) /* Connect PDx pins to FSMC Alternate function */ GPIOD->AFR[0] = 0x00cc00cc; - GPIOD->AFR[1] = 0xcc0ccccc; + //GPIOD->AFR[1] = 0xcc0ccccc; + GPIOD->AFR[1] = 0xcccccccc; //By TFT: enable PD13 as A18 /* Configure PDx pins in Alternate function mode */ - GPIOD->MODER = 0xa2aa0a0a; + //GPIOD->MODER = 0xa2aa0a0a; + GPIOD->MODER = 0xaaaa0a0a; //By TFT: enable PD13 as A18 /* Configure PDx pins speed to 100 MHz */ - GPIOD->OSPEEDR = 0xf3ff0f0f; + //GPIOD->OSPEEDR = 0xf3ff0f0f; + GPIOD->OSPEEDR = 0xffff0f0f; //By TFT: enable PD13 as A18 /* Configure PDx pins Output type to push-pull */ GPIOD->OTYPER = 0x00000000; /* No pull-up, pull-down for PDx pins */ GPIOD->PUPDR = 0x00000000; /* Connect PEx pins to FSMC Alternate function */ - GPIOE->AFR[0] = 0xc00000cc; + //GPIOE->AFR[0] = 0xc00000cc; + GPIOE->AFR[0] = 0xc00cc0cc; //By TFT: enable PE3,PE4 as A19,A20 GPIOE->AFR[1] = 0xcccccccc; /* Configure PEx pins in Alternate function mode */ - GPIOE->MODER = 0xaaaa800a; + //GPIOE->MODER = 0xaaaa800a; + GPIOE->MODER = 0xaaaa828a; //By TFT: enable PE3,PE4 as A19,A20 /* Configure PEx pins speed to 100 MHz */ - GPIOE->OSPEEDR = 0xffffc00f; + //GPIOE->OSPEEDR = 0xffffc00f; + GPIOE->OSPEEDR = 0xffffc3cf; //By TFT: enable PE3,PE4 as A19,A20 /* Configure PEx pins Output type to push-pull */ GPIOE->OTYPER = 0x00000000; /* No pull-up, pull-down for PEx pins */ diff --git a/miosix/arch/cortexM3_stm32f2/stm32f207ig_stm3220g-eval/stm3220g-eval.cfg b/miosix/arch/cortexM3_stm32f2/stm32f207ig_stm3220g-eval/stm3220g-eval.cfg index 0094fab62d7e8bfaecc4344f729517845f0b825d..d9fc56da30c0390fcbdde5a4791252ae662f783f 100644 --- a/miosix/arch/cortexM3_stm32f2/stm32f207ig_stm3220g-eval/stm3220g-eval.cfg +++ b/miosix/arch/cortexM3_stm32f2/stm32f207ig_stm3220g-eval/stm3220g-eval.cfg @@ -6,5 +6,5 @@ telnet_port 4444 gdb_port 3333 -source [find interface/olimex-arm-usb-ocd-h.cfg] +source [find interface/olimex-arm-usb-ocd.cfg] source [find board/stm3220g_eval.cfg] diff --git a/miosix/config/Makefile.inc b/miosix/config/Makefile.inc index e1e0a85d5b07fec67a13a8fd7cde1df47580c356..d4cdc74f0d57db6187aa32785c96426646d00fe2 100644 --- a/miosix/config/Makefile.inc +++ b/miosix/config/Makefile.inc @@ -27,8 +27,8 @@ OPT_BOARD := stm32f207ig_stm3220g-eval ## -O2 is recomended otherwise, as it provides a good balance between code ## size and speed ## -#OPT_OPTIMIZATION := -O0 -OPT_OPTIMIZATION := -O2 +OPT_OPTIMIZATION := -O0 +#OPT_OPTIMIZATION := -O2 #OPT_OPTIMIZATION := -O3 #OPT_OPTIMIZATION := -Os @@ -159,10 +159,10 @@ ifeq ($(OPT_BOARD),stm32f207ig_stm3220g-eval) ## Warning! enable external ram if you use a linker script that requires it ## (see the XRAM flag below) LINKER_SCRIPT_PATH := arch/cortexM3_stm32f2/stm32f207ig_stm3220g-eval/ - #LINKER_SCRIPT := $(LINKER_SCRIPT_PATH)stm32_1m+128k_rom.ld + LINKER_SCRIPT := $(LINKER_SCRIPT_PATH)stm32_1m+128k_rom.ld #LINKER_SCRIPT := $(LINKER_SCRIPT_PATH)stm32_1m+128k_xram.ld #LINKER_SCRIPT := $(LINKER_SCRIPT_PATH)stm32_1m+128k_all_in_xram.ld - LINKER_SCRIPT := $(LINKER_SCRIPT_PATH)stm32_1m+128k_all_in_xram_processes.ld + #LINKER_SCRIPT := $(LINKER_SCRIPT_PATH)stm32_1m+128k_all_in_xram_processes.ld ## Enable/disable initialization of external RAM at boot. Three options: ## __ENABLE_XRAM : If you want the heap in xram (with an appropriate linker @@ -171,8 +171,8 @@ ifeq ($(OPT_BOARD),stm32f207ig_stm3220g-eval) ## in xram (with an appropriate linker script selected above) ## none selected : don't use xram (with an appropriate linker script ## selected above) - #XRAM := -D__ENABLE_XRAM - XRAM := -D__ENABLE_XRAM -D__CODE_IN_XRAM + XRAM := -D__ENABLE_XRAM + #XRAM := -D__ENABLE_XRAM -D__CODE_IN_XRAM endif diff --git a/miosix/config/miosix_settings.h b/miosix/config/miosix_settings.h index 655c53101ee0f09c166eeb9e2a6dfbd7b1385695..0c87d019e4c2747f62b086d2d8f01423d301a410 100644 --- a/miosix/config/miosix_settings.h +++ b/miosix/config/miosix_settings.h @@ -67,7 +67,7 @@ namespace miosix { /// This enables the dynamic loader to load elf programs, the extended system /// call service and, if the hardware supports it, the MPU to provide memory /// isolation of processes -#define WITH_PROCESSES +//#define WITH_PROCESSES #if defined(WITH_PROCESSES) && defined(__NO_EXCEPTIONS) #error Processes require C++ exception support @@ -146,7 +146,7 @@ const unsigned int STACK_DEFAULT_FOR_PTHREAD=2048; /// Maximum size of the RAM image of a process. If a program requires more /// the kernel will not run it (MUST be divisible by 4) -const unsigned int MAX_PROCESS_IMAGE_SIZE=20*1024; +const unsigned int MAX_PROCESS_IMAGE_SIZE=64*1024; /// Minimum size of the stack for a process. If a program specifies a lower /// size the kernel will not run it (MUST be divisible by 4) diff --git a/miosix/kernel/elf_program.cpp b/miosix/kernel/elf_program.cpp index 6805d3944d429ef4e4073b428724a66f52db744c..7af6cf19633ecfbd4abf64c40ab3d86c5979d7a5 100644 --- a/miosix/kernel/elf_program.cpp +++ b/miosix/kernel/elf_program.cpp @@ -201,8 +201,12 @@ bool ElfProgram::validateDynamicSegment(const Elf32_Phdr *dynamic, throw runtime_error("Requested stack is too small"); if(ramSize>MAX_PROCESS_IMAGE_SIZE) throw runtime_error("Requested image size is too large"); - if((stackSize & 0x3) || (ramSize & 0x3) || - (stackSize>MAX_PROCESS_IMAGE_SIZE) || (dataSegmentSize+stackSize>ramSize)) + if((stackSize & 0x3) || + (ramSize & 0x3) || + (ramSize < ProcessPool::blockSize) || + (stackSize>MAX_PROCESS_IMAGE_SIZE) || + (dataSegmentSize>MAX_PROCESS_IMAGE_SIZE) || + (dataSegmentSize+stackSize>ramSize)) throw runtime_error("Invalid stack or RAM size"); if(hasRelocs!=0 && hasRelocs!=0x7) return false; @@ -242,7 +246,6 @@ void ProcessImage::load(const ElfProgram& program) { if(image) getProcessPool().deallocate(image); // if(image) delete[] image; - size=MAX_PROCESS_IMAGE_SIZE; const unsigned int base=program.getElfBase(); const Elf32_Phdr *phdr=program.getProgramHeaderTable(); const Elf32_Phdr *dataSegment=0; @@ -275,6 +278,7 @@ void ProcessImage::load(const ElfProgram& program) dtRelsz=dyn->d_un.d_val; break; case DT_MX_RAMSIZE: + size=dyn->d_un.d_val; image=getProcessPool().allocate(dyn->d_un.d_val); // image=new unsigned int[dyn->d_un.d_val/4]; default: @@ -320,7 +324,6 @@ void ProcessImage::load(const ElfProgram& program) ProcessImage::~ProcessImage() { - iprintf("%p\n",image); if(image) getProcessPool().deallocate(image); // if(image) delete[] image; } diff --git a/miosix/kernel/kernel.cpp b/miosix/kernel/kernel.cpp index 965d7980e5865b4b73532b39c855a2056a7ed14f..e50adb1b1419bf1ba6e04dc3cb42a4b6aa623f08 100644 --- a/miosix/kernel/kernel.cpp +++ b/miosix/kernel/kernel.cpp @@ -590,13 +590,6 @@ const int Thread::getStackSize() #ifdef WITH_PROCESSES -miosix_private::SyscallParameters Thread::switchToUserspace() -{ - miosix_private::portableSwitchToUserspace(); - miosix_private::SyscallParameters result(cur->userCtxsave); - return result; -} - void Thread::IRQhandleSvc(unsigned int svcNumber) { if(cur->proc==0) errorHandler(UNEXPECTED); @@ -671,6 +664,13 @@ void Thread::threadLauncher(void *(*threadfunc)(void*), void *argv) #ifdef WITH_PROCESSES +miosix_private::SyscallParameters Thread::switchToUserspace() +{ + miosix_private::portableSwitchToUserspace(); + miosix_private::SyscallParameters result(cur->userCtxsave); + return result; +} + Thread *Thread::createUserspace(void *(*startfunc)(void *), void *argv, unsigned short options, Process *proc) { @@ -728,10 +728,11 @@ Thread *Thread::createUserspace(void *(*startfunc)(void *), void *argv, } void Thread::setupUserspaceContext(unsigned int entry, unsigned int *gotBase, - unsigned int *stackTop) + unsigned int ramImageSize) { void *(*startfunc)(void*)=reinterpret_cast<void *(*)(void*)>(entry); - miosix_private::initCtxsave(cur->userCtxsave,startfunc,stackTop,0,gotBase); + unsigned int *ep=gotBase+ramImageSize/4; + miosix_private::initCtxsave(cur->userCtxsave,startfunc,ep,0,gotBase); } #endif //WITH_PROCESSES diff --git a/miosix/kernel/kernel.h b/miosix/kernel/kernel.h index cf8f9ae5c10e36ea56408b02007b23e7929bce86..c2abb0db80b79f9abe0ee19fd3a9fb4b33d794ec 100644 --- a/miosix/kernel/kernel.h +++ b/miosix/kernel/kernel.h @@ -694,17 +694,14 @@ public: #ifdef WITH_PROCESSES /** - * \return the syscall parameters - */ - static miosix_private::SyscallParameters switchToUserspace(); - - /** + * \internal * Can only be called inside an IRQ, its use is to switch a thread between * userspace/kernelspace and back to perform context switches */ static void IRQhandleSvc(unsigned int svcNumber); /** + * \internal * Can only be called inside an IRQ, its use is to report a fault so that * in case the fault has occurred within a process while it was executing * in userspace, the process can be terminated. @@ -875,6 +872,15 @@ private: #ifdef WITH_PROCESSES + /** + * \internal + * Causes a thread belonging to a process to switch to userspace, and + * execute userspace code. This function returns when the process performs + * a syscall or faults. + * \return the syscall parameters used to serve the system call. + */ + static miosix_private::SyscallParameters switchToUserspace(); + /** * Create a thread to be used inside a process. The thread is created in * WAIT status, a wakeup() on it is required to actually start it. @@ -889,14 +895,13 @@ private: /** * Setup the userspace context of the thread, so that it can be later * switched to userspace. Must be called only once for each thread instance - * Can only be called when the kernel is paused * \param entry userspace entry point * \param gotBase base address of the GOT, also corresponding to the start * of the RAM image of the process - * \param stackTop top of userspace stack + * \param ramImageSize size of the process ram image */ static void setupUserspaceContext(unsigned int entry, unsigned int *gotBase, - unsigned int *stackTop); + unsigned int ramImageSize); #endif //WITH_PROCESSES diff --git a/miosix/kernel/process.cpp b/miosix/kernel/process.cpp index fdaf9342fadf9a2650cad4aa361f257c950033c4..ec7e5bb70c4204eb830b24e2e6f77b91a94bd822 100644 --- a/miosix/kernel/process.cpp +++ b/miosix/kernel/process.cpp @@ -107,12 +107,11 @@ Process::Process(const ElfProgram& program) : program(program) void *Process::start(void *argv) { - Thread *thr=Thread::getCurrentThread(); - Process *proc=thr->proc; + Process *proc=Thread::getCurrentThread()->proc; if(proc==0) errorHandler(UNEXPECTED); Thread::setupUserspaceContext( proc->program.getEntryPoint(),proc->image.getProcessBasePointer(), - proc->image.getProcessBasePointer()+proc->image.getProcessImageSize()); + proc->image.getProcessImageSize()); bool running=true; do { miosix_private::SyscallParameters sp=Thread::switchToUserspace(); @@ -132,7 +131,9 @@ void *Process::start(void *argv) { case 2: running=false; + #ifdef WITH_ERRLOG iprintf("Exit %d\n",sp.getFirstParameter()); //FIXME: remove + #endif //WITH_ERRLOG break; case 3: //FIXME: check that the pointer belongs to the process diff --git a/miosix/kernel/process_pool.cpp b/miosix/kernel/process_pool.cpp index 0aa567f82bd4d1c52b2a8ccc8be3a0da8c782b8a..97e91b274f1f9f7fc9e911b4d6c5a8946ec79368 100644 --- a/miosix/kernel/process_pool.cpp +++ b/miosix/kernel/process_pool.cpp @@ -5,6 +5,10 @@ using namespace std; +#ifdef WITH_PROCESSES + +namespace miosix { + ProcessPool::ProcessPool(unsigned int *poolBase, unsigned int poolSize) : poolBase(poolBase), poolSize(poolSize) { @@ -106,3 +110,7 @@ int main() } } #endif //TEST_ALLOC + +} //namespace miosix + +#endif //WITH_PROCESSES diff --git a/miosix/kernel/process_pool.h b/miosix/kernel/process_pool.h index e21b97df7d75495bf68b7cfcdaa06c5e5dbdde33..bc101879d597e6dd296bb0be056265c47fb66ceb 100644 --- a/miosix/kernel/process_pool.h +++ b/miosix/kernel/process_pool.h @@ -1,4 +1,7 @@ +#ifndef PROCESS_POOL +#define PROCESS_POOL + #include <map> #ifndef TEST_ALLOC @@ -9,8 +12,9 @@ #include <sstream> #endif //TEST_ALLOC -#ifndef PROCESS_POOL -#define PROCESS_POOL +#ifdef WITH_PROCESSES + +namespace miosix { /** * This class allows to handle a memory area reserved for the allocation of @@ -131,4 +135,8 @@ private: #endif //TEST_ALLOC }; +} //namespace miosix + +#endif //WITH_PROCESSES + #endif //PROCESS_POOL diff --git a/miosix_np_2/nbproject/configurations.xml b/miosix_np_2/nbproject/configurations.xml index 758c673ca103f688d4ba6dbfa5b4fc254c009fa5..78743834e6c4e5359395684c221144defa997877 100644 --- a/miosix_np_2/nbproject/configurations.xml +++ b/miosix_np_2/nbproject/configurations.xml @@ -3,10 +3,8 @@ <logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT"> <df name="miosix_np_2" root=".."> <df name="app_template"> - <in>Makefile</in> <in>crt0.s</in> <in>main.c</in> - <in>miosix.ld</in> <in>prog3.h</in> </df> <df name="miosix">