From 7d6325a721fb43a9d850f12cf31df5b5b52a4398 Mon Sep 17 00:00:00 2001 From: leru <1138073902@qq.com> Date: Thu, 13 Nov 2025 17:20:40 +0800 Subject: [PATCH] add get core id to multicore implementation --- Interrupt/Makefile | 46 ------------ Interrupt/boot.ld | 52 -------------- Interrupt/boot.s | 171 --------------------------------------------- Interrupt/isr.c | 14 ---- Interrupt/main.cpp | 76 -------------------- Interrupt/timer.h | 8 --- Simple/Makefile | 1 + Simple/boot.ld | 2 +- Simple/boot.s | 18 ++++- Simple/main.cpp | 9 ++- common/Makefile | 6 +- 11 files changed, 30 insertions(+), 373 deletions(-) delete mode 100644 Interrupt/Makefile delete mode 100644 Interrupt/boot.ld delete mode 100644 Interrupt/boot.s delete mode 100644 Interrupt/isr.c delete mode 100644 Interrupt/main.cpp delete mode 100644 Interrupt/timer.h diff --git a/Interrupt/Makefile b/Interrupt/Makefile deleted file mode 100644 index fc2aa23..0000000 --- a/Interrupt/Makefile +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright (c) 2015, University of Kaiserslautern -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# 1. Redistributions of source code must retain the above copyright notice, -# this list of conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER -# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# -# Authors: Matthias Jung -# Frederik Lauer -include ../common/Makefile - -OBJS = boot.o ../common/syscalls.o main.o isr.o - -all: main.elf - -main.elf: $(OBJS) $(LNK_SCRIPT) Makefile - $(CC) $(LNK_FILE_OPT) -o $@ $(OBJS) $(LNK_OPT) - -boot.o: Makefile - $(CPP) boot.s $(CFLAGS) | $(AS) $(ASFLAGS) -o boot.o - -clean: - rm -f *.o *.elf diff --git a/Interrupt/boot.ld b/Interrupt/boot.ld deleted file mode 100644 index 57ba5b6..0000000 --- a/Interrupt/boot.ld +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2015, University of Kaiserslautern - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER - * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Authors: Matthias Jung - Frederik Lauer - */ - - -ENTRY(_Reset) -SECTIONS -{ - . = 0x0; - .text : { - boot.o (INTERRUPT_VECTOR) - *(.text) - } - .data : { *(.data) } - .bss : { *(.bss COMMON) } - . = ALIGN(8); - stack_base = .; - . = . + 0x1000; /* 4kB of stack memory*/ - . = . + 0x1000; /* 4kB of stack memory for IRQ*/ - PROVIDE (end = .) ; -} \ No newline at end of file diff --git a/Interrupt/boot.s b/Interrupt/boot.s deleted file mode 100644 index 30802ed..0000000 --- a/Interrupt/boot.s +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Copyright (c) 2015, University of Kaiserslautern - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER - * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Authors: Matthias Jung - * Frederik Lauer - */ - -.section INTERRUPT_VECTOR, "x" -.global _Reset -_Reset: - B Reset_Handler /* Reset */ - B . /* Undefined */ - B . /* SWI */ - B . /* Prefetch Abort */ - B . /* Data Abort */ - B . /* reserved */ - B irq_handler /* IRQ */ - B . /* FIQ */ - - -.equ Len_Stack, 0x1000; // 4kB of stack memory -.equ Len_IRQ_Stack, 0x1000; // 4kB of stack memory for IRQ Mode -//.equ stack_base, 0x18000 // stack_base defined in Linker Script - -//GIC Distributor & CPU interface -//GEM5_MACHINETYPE=VExpress_GEM5_V1 -.equ GIC_Dist_Base, 0x2c001000 -.equ GIC_CPU_BASE, 0x2c002000 - -//Register offsets -.equ set_enable1, 0x104 -.equ set_enable2, 0x108 - -//Example definitions -//GEM5_MACHINETYPE=VExpress_GEM5_V1 -.equ timer_irq_id, 57 // 57 <64 => set_enable1 Reg - -.equ GIC_CPU_mask_reg_offset, 0x04 -.equ GIC_CPU_Int_Ack_reg_offset, 0x0C -.equ GIC_CPU_End_of_int_offset, 0x10 - - -.global Reset_Handler -Reset_Handler: - // Set up stack pointers for IRQ processor mode - mov R1, #0b11010010 // interrupts masked, MODE = IRQ IRQ | FIQ | 0 | Mode[4:0] - msr CPSR, R1 // change to IRQ mode - ldr SP, =stack_base + Len_Stack + Len_IRQ_Stack // set IRQ stack - - // Change back to SVC (supervisor) mode with interrupts disabled - mov R1, #0b11010011 // interrupts masked, MODE = SVC IRQ | FIQ | 0 | Mode[4:0] - msr CPSR, R1 // change to SVC mode - ldr SP, =stack_base + Len_Stack // set stack - - // Enable individual interrupts, set target - bl config_gic_dist - - // Enable individual interrupts, set target - bl config_gic_cpu_interface - - // Enable interrupts in GIC Distributor - ldr r0, =GIC_Dist_Base - mov r1, #1 - str r1, [r0] - - // Enable IRQ interrupts in the processor: - mov R1, #0b01010011 // IRQ not masked (=0), MODE = SVC IRQ | FIQ | 0 | Mode[4:0] - msr CPSR, R1 - - bl main - B . - - -.global config_gic_dist -config_gic_dist: - push {lr} - /* Enable the Interrupt in the Set-Enable Register of the GIC Distributor - * Set-enable1 Reg Offset Address = 0x104 - * Bits 0 to 31 correspond to interrupt input lines 32 to 63 respectively. - * A bit set to 1 indicates an enabled interrupt. - * Set-enable2 Reg Offset Address = 0x108 - * Bits 0 to 31 correspond to interrupt input lines 64 to 95 respectively. - * A bit set to 1 indicates an enabled interrupt. - * This Example: Interrupt of timer0 => IRQ ID = 36 - */ - - ldr r1, =GIC_Dist_Base + set_enable1 // r1 = Set-enable1 Reg Address - mov r2, #1 - //GEM5_MACHINETYPE=VExpress_GEM5_V1 - //IRQ ID(57) - 32 = 25 - lsl r2, r2, #25 - ldr r3, [r1] // read current register value - orr r3, r3, r2 // set the enable bit - str r3, [r1] // store the new register value - - /* Configure Interrupt Processor Taget - * Reg offset 0x820 for ID32 − ID35 - * 0x824 for ID36 − ID39 - * ... - * default values are 0x01010101 => CPU0 is target for all. - */ - pop {pc} - - -.global config_gic_cpu_interface -config_gic_cpu_interface: - push {lr} - - // set Interrupt Priority mask (enable all priority levels) - ldr r1, =GIC_CPU_BASE + GIC_CPU_mask_reg_offset - ldr r2, =0xFFFF - str r2, [r1] - - // set the enable bit in the GIC_CPU_INTERFACE - mov r2, #1 - ldr r1, =GIC_CPU_BASE - str r2, [r1] - pop {pc} - - -// IRQ Handler that calls the ISR function in C -.global irq_handler -irq_handler: - push {r0-r7,lr} - - // Read the interrupt acknowledge register of the GIC_CPU_INTERFACE - ldr r1, =GIC_CPU_BASE + GIC_CPU_Int_Ack_reg_offset - ldr r2, [r1] - -irq_unexpected: - cmp r2, #timer_irq_id - bne irq_unexpected // if irq is not from timer0 - - // Jump to C - must clear the timer interrupt! - BL isr - - // write the IRQ ID to the END_OF_INTERRUPT Register of GIC_CPU_INTERFACE - ldr r1, =GIC_CPU_BASE + GIC_CPU_End_of_int_offset - ldr r2, =timer_irq_id - str r2, [r1] - - pop {r0-r7,lr} - subs pc, lr, #4 diff --git a/Interrupt/isr.c b/Interrupt/isr.c deleted file mode 100644 index 941983c..0000000 --- a/Interrupt/isr.c +++ /dev/null @@ -1,14 +0,0 @@ -#include - -#include "timer.h" - -void isr(void) -{ - //Timer BaseAddress - int *timer1 = (int*)TIMER_BASE; - - //Clear Timer Interrupt Flag - timer1[CNTP_CTL] = 0x02; - - printf("***************** INTERRUPT!!! *************************\n"); -} diff --git a/Interrupt/main.cpp b/Interrupt/main.cpp deleted file mode 100644 index c8d6602..0000000 --- a/Interrupt/main.cpp +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (c) 2015, University of Kaiserslautern - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER - * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * Authors: Matthias Jung - Frederik Lauer - */ - -#include - -#include "timer.h" - -#define TIMER_VALUE 0x1000 - -int main(void) -{ - /* This example configures a timer instance to trigger a - * Interrupt every few seconds. - * The Interrupt release the isr function in the isr.c - * file. - */ - - unsigned int delay = 0; - unsigned int counter = 0; - - int *timer1 = (int*)TIMER_BASE; - - timer1[CNTP_TVAL] = TIMER_VALUE; // set start value - - printf("##################################################\n"); - printf("################# Boot ##################\n"); - printf("##################################################\n"); - printf(" \n"); - - while (1) { - if (counter > 300000) { - counter = 0; - delay++; - if (delay == 5) { - printf("Start Timer Interrupts\n"); - timer1[CNTP_CTL] = 0x01; // start timer - delay = 1; - } - printf("Delay: %u\n",delay); - } else { - counter++; - } - } -} diff --git a/Interrupt/timer.h b/Interrupt/timer.h deleted file mode 100644 index e530b57..0000000 --- a/Interrupt/timer.h +++ /dev/null @@ -1,8 +0,0 @@ -/* Timer Register maps */ - -//GEM5_MACHINETYPE=VExpress_GEM5_V1 -#define TIMER_BASE 0x2a820000 - -//offsets -#define CNTP_CTL 11 -#define CNTP_TVAL 10 diff --git a/Simple/Makefile b/Simple/Makefile index afda21b..c3d5859 100644 --- a/Simple/Makefile +++ b/Simple/Makefile @@ -39,6 +39,7 @@ all: main.elf main.elf: $(OBJS) $(LNK_SCRIPT) Makefile $(CC) $(LNK_FILE_OPT) -o $@ $(OBJS) $(LNK_OPT) + cp *.elf ~/gem5_workspace boot.o: Makefile $(CPP) boot.s $(CFLAGS) | $(AS) $(ASFLAGS) -o boot.o diff --git a/Simple/boot.ld b/Simple/boot.ld index 252ac53..7780f9a 100644 --- a/Simple/boot.ld +++ b/Simple/boot.ld @@ -41,7 +41,7 @@ SECTIONS .data : { *(.data) } .bss : { *(.bss COMMON) } . = ALIGN(8); - . = . + 0x1000; /* 4kB of stack memory */ + . = . + 0x4000; /* 16kB of stack memory for multiple cores (4 cores * 4KB each) */ stack_top = .; PROVIDE (end = .) ; } diff --git a/Simple/boot.s b/Simple/boot.s index 516801a..50ed3b4 100644 --- a/Simple/boot.s +++ b/Simple/boot.s @@ -34,6 +34,22 @@ .global _Reset _Reset: - LDR sp, =stack_top + // 获取当前CPU的core id + MRC p15, 0, r0, c0, c0, 5 // 读取MPIDR寄存器 + AND r0, r0, #0xFF // 提取Affinity0字段(core id) + + // 设置栈指针,为每个core分配不同的栈空间 + LDR r1, =stack_top + MOV r2, #0x1000 // 每个core的栈大小为4KB + MUL r3, r0, r2 // 计算当前core的栈偏移 + SUB sp, r1, r3 // 设置栈指针 + BL main B . + +// 获取core id的函数 +.global get_core_id +get_core_id: + MRC p15, 0, r0, c0, c0, 5 // 读取MPIDR寄存器 + AND r0, r0, #0xFF // 提取Affinity0字段(core id) + BX lr diff --git a/Simple/main.cpp b/Simple/main.cpp index 5d2c547..430e422 100644 --- a/Simple/main.cpp +++ b/Simple/main.cpp @@ -34,11 +34,18 @@ #include +// 声明获取core id的汇编函数 +extern "C" unsigned int get_core_id(); + int main(void) { + unsigned int core_id = get_core_id(); unsigned int r = 1337; - printf("Hello World! %d\n", r); + + for(int i=0;i<1000*core_id;i++); + + printf("Hello World from Core %d! Value: %d\n", core_id, r); while (1) { diff --git a/common/Makefile b/common/Makefile index 5f64cb3..b1e0013 100644 --- a/common/Makefile +++ b/common/Makefile @@ -31,7 +31,7 @@ # Authors: Matthias Jung # Frederik Lauer -CROSS_COMPILE_DIR = /usr/local/bin +CROSS_COMPILE_DIR = /usr/bin ARCH ?= arm CROSS_COMPILE ?= arm-none-eabi- @@ -47,8 +47,8 @@ STRIP = $(CROSS_COMPILE_DIR)/$(CROSS_COMPILE)strip OBJCOPY = $(CROSS_COMPILE_DIR)/$(CROSS_COMPILE)objcopy OBJDUMP = $(CROSS_COMPILE_DIR)/$(CROSS_COMPILE)objdump -CFLAGS = -g -O3 -lunistd -I . -CXXFLAGS = -g -O3 -lunistd -I . -fno-exceptions +CFLAGS = -g -O0 -lunistd -I . +CXXFLAGS = -g -O0 -lunistd -I . -fno-exceptions ASFLAGS = -EL LNK_OPT = --specs=nosys.specs LNK_SCRIPT = boot.ld