Skip to content

Commit 7af710d

Browse files
committed
xtensa: add XIP kernel support
XIP (eXecute In Place) kernel image is the image that can be run directly from ROM, using RAM only for writable data. XIP xtensa kernel differs from regular xtensa kernel in the following ways: - it has exception/IRQ vectors merged into text section. No vectors relocation takes place at kernel startup. - .data/.bss location must be specified in the kernel configuration, its content is copied there in the _startup function. - .init.text is merged with the rest of text and is executed from ROM. - when MMU is used the virtual address where the kernel will be mapped must be specified in the kernel configuration. It may be in the KSEG or in the KIO, __pa macro is adjusted to be able to handle both. Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
1 parent 76743c0 commit 7af710d

10 files changed

Lines changed: 257 additions & 5 deletions

File tree

‎arch/xtensa/Kconfig‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ config XTENSA
1919
select GENERIC_PCI_IOMAP
2020
select GENERIC_SCHED_CLOCK
2121
select GENERIC_STRNCPY_FROM_USER if KASAN
22-
select HAVE_ARCH_JUMP_LABEL
23-
select HAVE_ARCH_KASAN if MMU
22+
select HAVE_ARCH_JUMP_LABEL if !XIP_KERNEL
23+
select HAVE_ARCH_KASAN if MMU && !XIP_KERNEL
2424
select HAVE_ARCH_TRACEHOOK
2525
select HAVE_DEBUG_KMEMLEAK
2626
select HAVE_DMA_CONTIGUOUS
@@ -299,6 +299,9 @@ config XTENSA_CALIBRATE_CCOUNT
299299
config SERIAL_CONSOLE
300300
def_bool n
301301

302+
config PLATFORM_HAVE_XIP
303+
def_bool n
304+
302305
menu "Platform options"
303306

304307
choice
@@ -325,6 +328,7 @@ config XTENSA_PLATFORM_XTFPGA
325328
select PLATFORM_WANT_DEFAULT_MEM if !MMU
326329
select SERIAL_CONSOLE
327330
select XTENSA_CALIBRATE_CCOUNT
331+
select PLATFORM_HAVE_XIP
328332
help
329333
XTFPGA is the name of Tensilica board family (LX60, LX110, LX200, ML605).
330334
This hardware is capable of running a full Linux distribution.
@@ -478,6 +482,27 @@ config INITIALIZE_XTENSA_MMU_INSIDE_VMLINUX
478482

479483
If in doubt, say Y.
480484

485+
config XIP_KERNEL
486+
bool "Kernel Execute-In-Place from ROM"
487+
depends on PLATFORM_HAVE_XIP
488+
help
489+
Execute-In-Place allows the kernel to run from non-volatile storage
490+
directly addressable by the CPU, such as NOR flash. This saves RAM
491+
space since the text section of the kernel is not loaded from flash
492+
to RAM. Read-write sections, such as the data section and stack,
493+
are still copied to RAM. The XIP kernel is not compressed since
494+
it has to run directly from flash, so it will take more space to
495+
store it. The flash address used to link the kernel object files,
496+
and for storing it, is configuration dependent. Therefore, if you
497+
say Y here, you must know the proper physical address where to
498+
store the kernel image depending on your own flash memory usage.
499+
500+
Also note that the make target becomes "make xipImage" rather than
501+
"make Image" or "make uImage". The final kernel binary to put in
502+
ROM memory will be arch/xtensa/boot/xipImage.
503+
504+
If unsure, say N.
505+
481506
config MEMMAP_CACHEATTR
482507
hex "Cache attributes for the memory address space"
483508
depends on !MMU
@@ -522,6 +547,16 @@ config KSEG_PADDR
522547

523548
If unsure, leave the default value here.
524549

550+
config KERNEL_VIRTUAL_ADDRESS
551+
hex "Kernel virtual address"
552+
depends on MMU && XIP_KERNEL
553+
default 0xd0003000
554+
help
555+
This is the virtual address where the XIP kernel is mapped.
556+
XIP kernel may be mapped into KSEG or KIO region, virtual address
557+
provided here must match kernel load address provided in
558+
KERNEL_LOAD_ADDRESS.
559+
525560
config KERNEL_LOAD_ADDRESS
526561
hex "Kernel load address"
527562
default 0x60003000 if !MMU
@@ -537,12 +572,21 @@ config KERNEL_LOAD_ADDRESS
537572
config VECTORS_OFFSET
538573
hex "Kernel vectors offset"
539574
default 0x00003000
575+
depends on !XIP_KERNEL
540576
help
541577
This is the offset of the kernel image from the relocatable vectors
542578
base.
543579

544580
If unsure, leave the default value here.
545581

582+
config XIP_DATA_ADDR
583+
hex "XIP kernel data virtual address"
584+
depends on XIP_KERNEL
585+
default 0x00000000
586+
help
587+
This is the virtual address where XIP kernel data is copied.
588+
It must be within KSEG if MMU is used.
589+
546590
config PLATFORM_WANT_DEFAULT_MEM
547591
def_bool n
548592

‎arch/xtensa/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ drivers-$(CONFIG_OPROFILE) += arch/xtensa/oprofile/
8787

8888
boot := arch/xtensa/boot
8989

90-
all Image zImage uImage: vmlinux
90+
all Image zImage uImage xipImage: vmlinux
9191
$(Q)$(MAKE) $(build)=$(boot) $@
9292

9393
archheaders:
@@ -97,4 +97,5 @@ define archhelp
9797
@echo '* Image - Kernel ELF image with reset vector'
9898
@echo '* zImage - Compressed kernel image (arch/xtensa/boot/images/zImage.*)'
9999
@echo '* uImage - U-Boot wrapped image'
100+
@echo ' xipImage - XIP image'
100101
endef

‎arch/xtensa/boot/Makefile‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ all: $(boot-y)
2929
Image: boot-elf
3030
zImage: boot-redboot
3131
uImage: $(obj)/uImage
32+
xipImage: $(obj)/xipImage
3233

3334
boot-elf boot-redboot: $(addprefix $(obj)/,$(subdir-y))
3435
$(Q)$(MAKE) $(build)=$(obj)/$@ $(MAKECMDGOALS)
@@ -50,3 +51,7 @@ UIMAGE_COMPRESSION = gzip
5051
$(obj)/uImage: vmlinux.bin.gz FORCE
5152
$(call if_changed,uimage)
5253
$(Q)$(kecho) ' Kernel: $@ is ready'
54+
55+
$(obj)/xipImage: vmlinux FORCE
56+
$(call if_changed,objcopy)
57+
$(Q)$(kecho) ' Kernel: $@ is ready'
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
CONFIG_SYSVIPC=y
2+
CONFIG_POSIX_MQUEUE=y
3+
CONFIG_NO_HZ_IDLE=y
4+
CONFIG_HIGH_RES_TIMERS=y
5+
CONFIG_PREEMPT=y
6+
CONFIG_IRQ_TIME_ACCOUNTING=y
7+
CONFIG_BSD_PROCESS_ACCT=y
8+
CONFIG_MEMCG=y
9+
CONFIG_CGROUP_FREEZER=y
10+
CONFIG_CGROUP_DEVICE=y
11+
CONFIG_CGROUP_CPUACCT=y
12+
CONFIG_CGROUP_DEBUG=y
13+
CONFIG_NAMESPACES=y
14+
CONFIG_SCHED_AUTOGROUP=y
15+
CONFIG_RELAY=y
16+
CONFIG_BLK_DEV_INITRD=y
17+
CONFIG_EXPERT=y
18+
CONFIG_KALLSYMS_ALL=y
19+
CONFIG_PROFILING=y
20+
CONFIG_XTENSA_VARIANT_DC233C=y
21+
CONFIG_XTENSA_UNALIGNED_USER=y
22+
CONFIG_XIP_KERNEL=y
23+
CONFIG_XIP_DATA_ADDR=0xd0000000
24+
CONFIG_KERNEL_VIRTUAL_ADDRESS=0xe6000000
25+
CONFIG_KERNEL_LOAD_ADDRESS=0xf6000000
26+
CONFIG_XTENSA_KSEG_512M=y
27+
CONFIG_HIGHMEM=y
28+
CONFIG_XTENSA_PLATFORM_XTFPGA=y
29+
CONFIG_CMDLINE_BOOL=y
30+
CONFIG_CMDLINE="earlycon=uart8250,mmio32native,0xfd050020,115200n8 console=ttyS0,115200n8 ip=dhcp root=/dev/nfs rw debug memmap=0x38000000@0"
31+
CONFIG_USE_OF=y
32+
CONFIG_BUILTIN_DTB_SOURCE="kc705"
33+
# CONFIG_PARSE_BOOTPARAM is not set
34+
CONFIG_OPROFILE=y
35+
# CONFIG_CORE_DUMP_DEFAULT_ELF_HEADERS is not set
36+
# CONFIG_COMPACTION is not set
37+
CONFIG_NET=y
38+
CONFIG_PACKET=y
39+
CONFIG_UNIX=y
40+
CONFIG_INET=y
41+
CONFIG_IP_MULTICAST=y
42+
CONFIG_IP_PNP=y
43+
CONFIG_IP_PNP_DHCP=y
44+
CONFIG_IP_PNP_BOOTP=y
45+
CONFIG_IP_PNP_RARP=y
46+
# CONFIG_IPV6 is not set
47+
CONFIG_NETFILTER=y
48+
# CONFIG_WIRELESS is not set
49+
CONFIG_UEVENT_HELPER=y
50+
CONFIG_DEVTMPFS=y
51+
CONFIG_DEVTMPFS_MOUNT=y
52+
# CONFIG_STANDALONE is not set
53+
CONFIG_BLK_DEV_LOOP=y
54+
CONFIG_BLK_DEV_RAM=y
55+
CONFIG_SCSI=y
56+
CONFIG_BLK_DEV_SD=y
57+
CONFIG_NETDEVICES=y
58+
# CONFIG_NET_VENDOR_ARC is not set
59+
# CONFIG_NET_VENDOR_AURORA is not set
60+
# CONFIG_NET_VENDOR_BROADCOM is not set
61+
# CONFIG_NET_VENDOR_INTEL is not set
62+
# CONFIG_NET_VENDOR_MARVELL is not set
63+
# CONFIG_NET_VENDOR_MICREL is not set
64+
# CONFIG_NET_VENDOR_NATSEMI is not set
65+
# CONFIG_NET_VENDOR_SAMSUNG is not set
66+
# CONFIG_NET_VENDOR_SEEQ is not set
67+
# CONFIG_NET_VENDOR_SMSC is not set
68+
# CONFIG_NET_VENDOR_STMICRO is not set
69+
# CONFIG_NET_VENDOR_VIA is not set
70+
# CONFIG_NET_VENDOR_WIZNET is not set
71+
CONFIG_MARVELL_PHY=y
72+
# CONFIG_WLAN is not set
73+
# CONFIG_INPUT_KEYBOARD is not set
74+
# CONFIG_INPUT_MOUSE is not set
75+
# CONFIG_SERIO is not set
76+
CONFIG_DEVKMEM=y
77+
CONFIG_SERIAL_8250=y
78+
# CONFIG_SERIAL_8250_DEPRECATED_OPTIONS is not set
79+
CONFIG_SERIAL_8250_CONSOLE=y
80+
CONFIG_SERIAL_OF_PLATFORM=y
81+
# CONFIG_HWMON is not set
82+
CONFIG_WATCHDOG=y
83+
CONFIG_WATCHDOG_NOWAYOUT=y
84+
CONFIG_SOFT_WATCHDOG=y
85+
# CONFIG_VGA_CONSOLE is not set
86+
# CONFIG_USB_SUPPORT is not set
87+
# CONFIG_IOMMU_SUPPORT is not set
88+
CONFIG_EXT3_FS=y
89+
CONFIG_FANOTIFY=y
90+
CONFIG_VFAT_FS=y
91+
CONFIG_PROC_KCORE=y
92+
CONFIG_TMPFS=y
93+
CONFIG_TMPFS_POSIX_ACL=y
94+
CONFIG_NFS_FS=y
95+
CONFIG_NFS_V4=y
96+
CONFIG_NFS_SWAP=y
97+
CONFIG_ROOT_NFS=y
98+
CONFIG_SUNRPC_DEBUG=y
99+
CONFIG_NLS_CODEPAGE_437=y
100+
CONFIG_NLS_ISO8859_1=y
101+
CONFIG_CRYPTO_ECHAINIV=y
102+
CONFIG_CRYPTO_DEFLATE=y
103+
CONFIG_CRYPTO_LZO=y
104+
CONFIG_CRYPTO_ANSI_CPRNG=y
105+
CONFIG_PRINTK_TIME=y
106+
CONFIG_DYNAMIC_DEBUG=y
107+
CONFIG_DEBUG_INFO=y
108+
CONFIG_MAGIC_SYSRQ=y
109+
CONFIG_DETECT_HUNG_TASK=y
110+
# CONFIG_SCHED_DEBUG is not set
111+
CONFIG_SCHEDSTATS=y
112+
CONFIG_DEBUG_RT_MUTEXES=y
113+
CONFIG_DEBUG_SPINLOCK=y
114+
CONFIG_DEBUG_MUTEXES=y
115+
CONFIG_DEBUG_ATOMIC_SLEEP=y
116+
CONFIG_STACKTRACE=y
117+
CONFIG_RCU_TRACE=y
118+
# CONFIG_FTRACE is not set
119+
# CONFIG_S32C1I_SELFTEST is not set

‎arch/xtensa/include/asm/cache.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@
3131

3232
#define ARCH_DMA_MINALIGN L1_CACHE_BYTES
3333

34+
/*
35+
* R/O after init is actually writable, it cannot go to .rodata
36+
* according to vmlinux linker script.
37+
*/
38+
#define __ro_after_init __read_mostly
39+
3440
#endif /* _XTENSA_CACHE_H */

‎arch/xtensa/include/asm/page.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,18 @@ static inline unsigned long ___pa(unsigned long va)
169169
if (off >= XCHAL_KSEG_SIZE)
170170
off -= XCHAL_KSEG_SIZE;
171171

172+
#ifndef CONFIG_XIP_KERNEL
172173
return off + PHYS_OFFSET;
174+
#else
175+
if (off < XCHAL_KSEG_SIZE)
176+
return off + PHYS_OFFSET;
177+
178+
off -= XCHAL_KSEG_SIZE;
179+
if (off >= XCHAL_KIO_SIZE)
180+
off -= XCHAL_KIO_SIZE;
181+
182+
return off + XCHAL_KIO_PADDR;
183+
#endif
173184
}
174185
#define __pa(x) ___pa((unsigned long)(x))
175186
#else

‎arch/xtensa/include/asm/vectors.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
#include <asm/kmem_layout.h>
2323

2424
#if defined(CONFIG_MMU) && XCHAL_HAVE_PTP_MMU && XCHAL_HAVE_SPANNING_WAY
25+
#ifdef CONFIG_KERNEL_VIRTUAL_ADDRESS
26+
#define KERNELOFFSET CONFIG_KERNEL_VIRTUAL_ADDRESS
27+
#else
2528
#define KERNELOFFSET (CONFIG_KERNEL_LOAD_ADDRESS + \
2629
XCHAL_KSEG_CACHED_VADDR - \
2730
XCHAL_KSEG_PADDR)
31+
#endif
2832
#else
2933
#define KERNELOFFSET CONFIG_KERNEL_LOAD_ADDRESS
3034
#endif

‎arch/xtensa/kernel/head.S‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,13 @@ ENTRY(_startup)
260260
___invalidate_icache_all a2 a3
261261
isync
262262

263+
#ifdef CONFIG_XIP_KERNEL
264+
/* Setup bootstrap CPU stack in XIP kernel */
265+
266+
movi a1, start_info
267+
l32i a1, a1, 0
268+
#endif
269+
263270
movi a6, 0
264271
xsr a6, excsave1
265272

‎arch/xtensa/kernel/setup.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ extern char _Level6InterruptVector_text_end;
308308
extern char _SecondaryResetVector_text_start;
309309
extern char _SecondaryResetVector_text_end;
310310
#endif
311+
#ifdef CONFIG_XIP_KERNEL
312+
extern char _xip_start[];
313+
extern char _xip_end[];
314+
#endif
311315

312316
static inline int __init_memblock mem_reserve(unsigned long start,
313317
unsigned long end)
@@ -339,6 +343,9 @@ void __init setup_arch(char **cmdline_p)
339343
#endif
340344

341345
mem_reserve(__pa(_stext), __pa(_end));
346+
#ifdef CONFIG_XIP_KERNEL
347+
mem_reserve(__pa(_xip_start), __pa(_xip_end));
348+
#endif
342349

343350
#ifdef CONFIG_VECTORS_OFFSET
344351
mem_reserve(__pa(&_WindowVectors_text_start),

0 commit comments

Comments
 (0)