diff --git a/arch/arm/src/qemu/CMakeLists.txt b/arch/arm/src/qemu/CMakeLists.txt index 7f49bbc220..0259af1566 100644 --- a/arch/arm/src/qemu/CMakeLists.txt +++ b/arch/arm/src/qemu/CMakeLists.txt @@ -38,4 +38,8 @@ if(CONFIG_BUILD_KERNEL) list(APPEND SRCS qemu_pgalloc.c) endif() +if(CONFIG_BUILD_PROTECTED) + list(APPEND SRCS qemu_userspace.c) +endif() + target_sources(arch PRIVATE ${SRCS}) diff --git a/arch/arm/src/qemu/Make.defs b/arch/arm/src/qemu/Make.defs index 0881042249..19b47ec894 100644 --- a/arch/arm/src/qemu/Make.defs +++ b/arch/arm/src/qemu/Make.defs @@ -42,3 +42,7 @@ endif ifeq ($(CONFIG_ARCH_IDLE_CUSTOM),y) CHIP_CSRCS += qemu_idle.c endif + +ifeq ($(CONFIG_BUILD_PROTECTED),y) + CHIP_CSRCS += qemu_userspace.c +endif diff --git a/arch/arm/src/qemu/qemu_boot.c b/arch/arm/src/qemu/qemu_boot.c index d64db2bbfa..f45b980faf 100644 --- a/arch/arm/src/qemu/qemu_boot.c +++ b/arch/arm/src/qemu/qemu_boot.c @@ -34,6 +34,7 @@ #include "qemu_irq.h" #include "qemu_memorymap.h" +#include "qemu_userspace.h" #include "smp.h" #include "gic.h" @@ -108,6 +109,10 @@ void arm_boot(void) syslog_rpmsg_init_early(g_syslog_rpmsg_buf, sizeof(g_syslog_rpmsg_buf)); #endif +#ifdef CONFIG_BUILD_PROTECTED + qemu_userspace(); +#endif + #ifdef CONFIG_ARCH_ARMV7R /* dont return per armv7-r/arm_head.S design */ diff --git a/arch/arm/src/qemu/qemu_userspace.c b/arch/arm/src/qemu/qemu_userspace.c new file mode 100644 index 0000000000..f4c1645be2 --- /dev/null +++ b/arch/arm/src/qemu/qemu_userspace.c @@ -0,0 +1,109 @@ +/**************************************************************************** + * arch/arm/src/qemu/qemu_userspace.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include + +#include + +#include "mpu.h" +#include "qemu_userspace.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: qemu_userspace + * + * Description: + * For the case of the separate user-/kernel-space build, perform whatever + * platform specific initialization of the user memory is required. + * Normally this just means initializing the user space .data and .bss + * segments. + * + * Assumptions: + * The D-Cache has not yet been enabled. + * + ****************************************************************************/ + +void qemu_userspace(void) +{ + uint8_t *src; + uint8_t *dest; + uint8_t *end; + uintptr_t datastart; + uintptr_t dataend; + + /* Clear all of user-space .bss */ + + DEBUGASSERT(USERSPACE->us_bssstart != 0 && + USERSPACE->us_bssend != 0 && + USERSPACE->us_bssstart <= USERSPACE->us_bssend); + + dest = (uint8_t *)USERSPACE->us_bssstart; + end = (uint8_t *)USERSPACE->us_bssend; + + while (dest != end) + { + *dest++ = 0; + } + + /* Initialize all of user-space .data */ + + DEBUGASSERT(USERSPACE->us_datasource != 0 && + USERSPACE->us_datastart != 0 && + USERSPACE->us_dataend != 0 && + USERSPACE->us_datastart <= USERSPACE->us_dataend); + + src = (uint8_t *)USERSPACE->us_datasource; + dest = (uint8_t *)USERSPACE->us_datastart; + end = (uint8_t *)USERSPACE->us_dataend; + + while (dest != end) + { + *dest++ = *src++; + } + + DEBUGASSERT(USERSPACE->us_textend >= USERSPACE->us_textstart); + + datastart = MIN(USERSPACE->us_datastart, USERSPACE->us_bssstart); + dataend = MAX(USERSPACE->us_dataend, USERSPACE->us_bssend); + + DEBUGASSERT(dataend >= datastart); + + /* Configure user FLASH and SRAM spaces */ + + mpu_showtype(); + mpu_user_flash(UFLASH_START, UFLASH_SIZE); + mpu_user_intsram(USRAM_START, USRAM_SIZE); + mpu_control(true); +} diff --git a/arch/arm/src/qemu/qemu_userspace.h b/arch/arm/src/qemu/qemu_userspace.h new file mode 100644 index 0000000000..8acfd21d63 --- /dev/null +++ b/arch/arm/src/qemu/qemu_userspace.h @@ -0,0 +1,78 @@ +/**************************************************************************** + * arch/arm/src/qemu/qemu_userspace.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_ARM_SRC_QEMU_QEMU_USERSPACE_H +#define __ARCH_ARM_SRC_QEMU_QEMU_USERSPACE_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include +#include +#include + +#include "arm_internal.h" +#include "chip.h" + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifndef __ASSEMBLY__ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: qemu_userspace + * + * Description: + * For the case of the separate user-/kernel-space build, perform whatever + * platform specific initialization of the user memory is required. + * Normally this just means initializing the user space .data and .bss + * segments. + * + ****************************************************************************/ + +void qemu_userspace(void); + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __ARCH_ARM_SRC_QEMU_QEMU_USERSPACE_H */