samples: code_relocation: An example for code relocation feature.

This sample provides an example for using the code relocation
feature. This example will place text,data,bss from 3 files to
various parts in the SRAM. For this a custom linker file is used
which is derived from include/arch/arm/cortex_m/scripts/linker.ld.

Signed-off-by: Varun Sharma <varun.sharma@intel.com>
Signed-off-by: Adithya Baglody <adithya.nagaraj.baglody@intel.com>
This commit is contained in:
Adithya Baglody 2018-11-26 20:50:23 +05:30 committed by Anas Nashif
commit 21fa43387e
9 changed files with 207 additions and 0 deletions

View file

@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.8.2)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(test_relocation)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
# Code relocation feature
zephyr_code_relocate(src/test_file1.c SRAM2)
zephyr_code_relocate(src/test_file2.c SRAM)
zephyr_code_relocate(src/test_file3.c SRAM2_TEXT)
zephyr_code_relocate(src/test_file3.c SRAM_DATA)
zephyr_code_relocate(src/test_file3.c SRAM2_BSS)
zephyr_code_relocate(../../../kernel/sem.c SRAM)

View file

@ -0,0 +1,4 @@
SECTION_DATA_PROLOGUE(_CUSTOM_SECTION_NAME2,,)
{
KEEP(*(".custom_section.*"));
} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2013-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Linker command/script file
*
* Linker script for the Cortex-M platforms.
*/
#define _LINKER
#define _ASMLANGUAGE
#include <autoconf.h>
#include <linker/sections.h>
#include <generated_dts_board.h>
#include <linker/linker-defs.h>
#include <linker/linker-tool.h>
/** enable CONFIG_SRAM2 or any other partition in soc Kconfig,
* this is just an example to show relocation of code/data/bss script
*/
#if defined CONFIG_ARM
#define CONFIG_SRAM2 1
#define _SRAM2_DATA_SECTION_NAME .sram2_data
#define _SRAM2_BSS_SECTION_NAME .sram2_bss
#define _SRAM2_TEXT_SECTION_NAME .sram2_text
#endif
#define RAM_SIZE2 (CONFIG_SRAM_SIZE * 512)
MEMORY
{
#ifdef CONFIG_SRAM2
SRAM2 (wx) : ORIGIN = (CONFIG_SRAM_BASE_ADDRESS + RAM_SIZE2), LENGTH = RAM_SIZE2
#endif
}
#include <arch/arm/cortex_m/scripts/linker.ld>

View file

@ -0,0 +1,4 @@
CONFIG_CODE_DATA_RELOCATION=y
CONFIG_HAVE_CUSTOM_LINKER_SCRIPT=y
CONFIG_CUSTOM_LINKER_SCRIPT="linker_arm_sram2.ld"
CONFIG_CUSTOM_SECTIONS_LD=y

View file

@ -0,0 +1,12 @@
sample:
description: Code data relocation Sample
name: code relocation
tests:
test:
harness: console
harness_config:
type: one_line
regex:
- "Hello World! (.*)"
platform_whitelist: qemu_cortex_m3 mps2_an385 sam_e70_xplained
tags: linker

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2018 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <kernel.h>
#include <misc/printk.h>
/* This function will allow execute from sram region.
* This is needed only for this sample because by default all soc will
* disable the execute from SRAM.
* An application that requires that the code be executed from SRAM will
* have to configure the region appropriately in arm_mpu_regions.c.
*/
#ifdef CONFIG_ARM_MPU
#include <arch/arm/cortex_m/cmsis.h>
void disable_mpu_rasr_xn(void)
{
u32_t index;
/* Kept the max index as 8(irrespective of soc) because the sram
* would most likely be set at index 2.
*/
for (index = 0; index < 8; index++) {
MPU->RNR = index;
if (MPU->RASR & MPU_RASR_XN_Msk) {
MPU->RASR ^= MPU_RASR_XN_Msk;
}
}
}
#endif /* CONFIG_ARM_MPU */
extern void function_in_sram2(void);
extern void function_in_split_multiple(void);
void main(void)
{
#ifdef CONFIG_ARM_MPU
disable_mpu_rasr_xn();
#endif /* CONFIG_ARM_MPU */
printk("Address of main function %p\n\n", &main);
function_in_sram2();
function_in_split_multiple();
}

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <misc/printk.h>
u32_t var_sram2_data = 10U;
u32_t var_sram2_bss;
K_SEM_DEFINE(test, 0, 1);
const u32_t var_sram2_rodata = 100U;
__in_section(custom_section, static, var) u32_t var_custom_data = 1U;
extern void function_in_sram(s32_t value);
void function_in_custom_section(void);
void function_in_sram2(void)
{
/* Print values from sram2 */
printk("Address of function_in_sram2 %p\n", &function_in_sram2);
printk("Address of var_sram2_data %p\n", &var_sram2_data);
printk("Address of k_sem_give %p\n", &k_sem_give);
printk("Address of var_sram2_rodata %p\n", &var_sram2_rodata);
printk("Address of var_sram2_bss %p\n\n", &var_sram2_bss);
/* Print values from sram */
function_in_sram(var_sram2_data);
/* Print values which were placed using attributes */
printk("Address of custom_section, func placed using attributes %p\n",
&function_in_custom_section);
printk("Address of custom_section data placed using attributes %p\n\n",
&var_custom_data);
k_sem_give(&test);
}
__in_section(custom_section, static, fun) void function_in_custom_section(void)
{
return;
}

View file

@ -0,0 +1,14 @@
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <misc/printk.h>
void function_in_sram(s32_t value)
{
printk("Address of function_in_sram %p\n", &function_in_sram);
printk("Hello World! %s\n", CONFIG_BOARD);
}

View file

@ -0,0 +1,19 @@
/*
* Copyright (c) 2012-2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <misc/printk.h>
u32_t var_file3_sram_data = 10U;
u32_t var_file3_sram2_bss;
void function_in_split_multiple(void)
{
printk("Address of function_in_split_multiple %p\n",
&function_in_split_multiple);
printk("Address of var_file3_sram_data %p\n", &var_file3_sram_data);
printk("Address of var_file3_sram2_bss %p\n\n", &var_file3_sram2_bss);
}