-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Initial port to powerpc bare metal #5093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
include ../../py/mkenv.mk | ||
|
||
# qstr definitions (must come before including py.mk) | ||
QSTR_DEFS = qstrdefsport.h | ||
|
||
# include py core make definitions | ||
include $(TOP)/py/py.mk | ||
|
||
ARCH = $(shell uname -m) | ||
ifneq ("$(ARCH)", "ppc64") | ||
ifneq ("$(ARCH)", "ppc64le") | ||
CROSS_COMPILE = powerpc64le-linux- | ||
endif | ||
endif | ||
|
||
INC += -I. | ||
INC += -I$(TOP) | ||
INC += -I$(BUILD) | ||
|
||
CFLAGS = $(INC) -g -Wall -std=c99 $(COPT) | ||
CFLAGS += -mno-string -mno-multiple -mno-vsx -mno-altivec -nostdlib | ||
CFLAGS += -mlittle-endian -mstrict-align -msoft-float | ||
CFLAGS += -Os | ||
CFLAGS += -fdata-sections -ffunction-sections -fno-stack-protector -ffreestanding | ||
CFLAGS += -U_FORTIFY_SOURCE | ||
|
||
LDFLAGS = -N -T powerpc.lds -nostdlib | ||
|
||
LIBS = | ||
|
||
SRC_C = \ | ||
main.c \ | ||
uart_core.c \ | ||
uart_potato.c \ | ||
uart_lpc_serial.c \ | ||
lib/utils/printf.c \ | ||
lib/utils/stdout_helpers.c \ | ||
lib/utils/pyexec.c \ | ||
lib/libc/string0.c \ | ||
lib/mp-readline/readline.c \ | ||
$(BUILD)/_frozen_mpy.c \ | ||
|
||
CLEAN_EXTRA = head.o | ||
|
||
OBJ = $(PY_CORE_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) | ||
|
||
all: $(BUILD)/firmware.elf $(BUILD)/firmware.map $(BUILD)/firmware.bin | ||
|
||
$(BUILD)/_frozen_mpy.c: frozentest.mpy $(BUILD)/genhdr/qstrdefs.generated.h | ||
$(ECHO) "MISC freezing bytecode" | ||
$(Q)$(TOP)/tools/mpy-tool.py -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h -mlongint-impl=mpz $< > $@ | ||
|
||
head.o : head.S | ||
$(Q)$(CC) head.S -c -o head.o | ||
|
||
$(BUILD)/firmware.elf: $(OBJ) head.o powerpc.lds | ||
$(ECHO) "LINK $@" | ||
$(Q)$(LD) $(LDFLAGS) -o $@ $^ $(LIBS) | ||
$(Q)$(SIZE) $@ | ||
|
||
$(BUILD)/firmware.bin: $(BUILD)/firmware.elf | ||
$(Q)$(OBJCOPY) -O binary $^ $(BUILD)/firmware.bin | ||
|
||
$(BUILD)/firmware.map: $(BUILD)/firmware.elf | ||
$(Q)nm $^ | sort > $(BUILD)/firmware.map | ||
|
||
include $(TOP)/py/mkrules.mk |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# The PowerPC port that runs on microwatt and qemu | ||
|
||
This port is intended to be a minimal MicroPython port that runs in | ||
QEMU, microwatt simulator with ghdl or microwatt on Xilinx FPGA with | ||
potato UART. | ||
|
||
## Building | ||
|
||
By default the port will be built for the host machine: | ||
|
||
$ make | ||
|
||
## Cross compilation for POWERPC | ||
|
||
If you need to cross compilers you'll want to grab a powerpc64le | ||
compiler (not powerpc or powerpc64). | ||
|
||
On Ubuntu (18.04) you'll want: | ||
|
||
$ apt install gcc-powerpc64le-linux-gnu | ||
|
||
*(Use CROSS_COMPILE=powerpc64le-linux-gnu-)* | ||
|
||
If your distro doesn't have cross compilers, you can get cross compilers here: | ||
- https://toolchains.bootlin.com/ | ||
*(use CROSS_COMPILE=powerpc64le-buildroot-linux-gnu-)* | ||
|
||
(Avoid musl libc as it defines __assert_fail() differently to glibc | ||
which breaks the micropython powerpc code) | ||
|
||
Then do: | ||
|
||
$ make CROSS_COMPILE=<compiler prefix> | ||
|
||
Building will produce the build/firmware.bin file which can be used | ||
QEMU or [microwatt](https://github.com/antonblanchard/microwatt). | ||
|
||
To run in QEMU use: | ||
|
||
$ ./qemu-system-ppc64 -M powernv -cpu POWER9 -nographic -bios build/firmware.bin |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
print('uPy') | ||
print('a long string that is not interned') | ||
print('a string that has unicode αβγ chars') | ||
print(b'bytes 1234\x01') | ||
print(123456789) | ||
for i in range(4): | ||
print(i) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2019, Michael Neuling, IBM Corporation. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#define STACK_TOP 0x60000 | ||
|
||
#define FIXUP_ENDIAN \ | ||
tdi 0,0,0x48; /* Reverse endian of b . + 8 */ \ | ||
b 191f; /* Skip trampoline if endian is good */ \ | ||
.long 0xa600607d; /* mfmsr r11 */ \ | ||
.long 0x01006b69; /* xori r11,r11,1 */ \ | ||
.long 0x05009f42; /* bcl 20,31,$+4 */ \ | ||
.long 0xa602487d; /* mflr r10 */ \ | ||
.long 0x14004a39; /* addi r10,r10,20 */ \ | ||
.long 0xa64b5a7d; /* mthsrr0 r10 */ \ | ||
.long 0xa64b7b7d; /* mthsrr1 r11 */ \ | ||
.long 0x2402004c; /* hrfid */ \ | ||
191: | ||
|
||
|
||
/* Load an immediate 64-bit value into a register */ | ||
#define LOAD_IMM64(r, e) \ | ||
lis r,(e)@highest; \ | ||
ori r,r,(e)@higher; \ | ||
rldicr r,r, 32, 31; \ | ||
oris r,r, (e)@h; \ | ||
ori r,r, (e)@l; | ||
|
||
.section ".head","ax" | ||
|
||
/* | ||
* Microwatt comes in at 0 as little endian so we do not need to worry up | ||
* FIXUP_ENDIAN. | ||
*/ | ||
. = 0 | ||
.global _start | ||
_start: | ||
b boot_entry | ||
|
||
/* QEMU comes in at 0x10. Put a value in argc/r3 to distingush from | ||
* microwatt. */ | ||
. = 0x10 | ||
FIXUP_ENDIAN | ||
LOAD_IMM64(%r3, 1) | ||
b boot_entry | ||
|
||
.global boot_entry | ||
boot_entry: | ||
/* Save R3 to non-volatile register */ | ||
mr %r14, %r3 | ||
restart: | ||
/* | ||
* setup stack with a safety gap, since we might write to the | ||
* previous frame. | ||
*/ | ||
LOAD_IMM64(%r1, STACK_TOP - 0x100) | ||
LOAD_IMM64(%r12, main) | ||
mtctr %r12 | ||
bctrl | ||
|
||
/* On exit, restart */ | ||
mr %r3, %r14 | ||
b restart | ||
|
||
#define EXCEPTION(nr) \ | ||
.= nr ;\ | ||
b . | ||
|
||
/* More exception stubs */ | ||
EXCEPTION(0x300) | ||
EXCEPTION(0x380) | ||
EXCEPTION(0x400) | ||
EXCEPTION(0x480) | ||
EXCEPTION(0x500) | ||
EXCEPTION(0x600) | ||
EXCEPTION(0x700) | ||
EXCEPTION(0x800) | ||
EXCEPTION(0x900) | ||
EXCEPTION(0x980) | ||
EXCEPTION(0xa00) | ||
EXCEPTION(0xb00) | ||
EXCEPTION(0xc00) | ||
EXCEPTION(0xd00) | ||
EXCEPTION(0xe00) | ||
EXCEPTION(0xe20) | ||
EXCEPTION(0xe40) | ||
EXCEPTION(0xe60) | ||
EXCEPTION(0xe80) | ||
EXCEPTION(0xf00) | ||
EXCEPTION(0xf20) | ||
EXCEPTION(0xf40) | ||
EXCEPTION(0xf60) | ||
EXCEPTION(0xf80) | ||
EXCEPTION(0x1000) | ||
EXCEPTION(0x1100) | ||
EXCEPTION(0x1200) | ||
EXCEPTION(0x1300) | ||
EXCEPTION(0x1400) | ||
EXCEPTION(0x1500) | ||
EXCEPTION(0x1600) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* This file is part of the MicroPython project, http://micropython.org/ | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2019, Michael Neuling, IBM Corporation. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
#include "py/compile.h" | ||
#include "py/runtime.h" | ||
#include "py/repl.h" | ||
#include "py/gc.h" | ||
#include "py/mperrno.h" | ||
#include "py/stackctrl.h" | ||
#include "lib/utils/pyexec.h" | ||
|
||
void __stack_chk_fail(void); | ||
void __stack_chk_fail(void) { | ||
static bool failed_once; | ||
|
||
if (failed_once) { | ||
return; | ||
} | ||
failed_once = true; | ||
printf("Stack corruption detected !\n"); | ||
assert(0); | ||
} | ||
|
||
/* fill in __assert_fail for libc */ | ||
void __assert_fail(const char *__assertion, const char *__file, | ||
unsigned int __line, const char *__function) { | ||
printf("Assert at %s:%d:%s() \"%s\" failed\n", __file, __line, __function, __assertion); | ||
for (;;) ; | ||
} | ||
|
||
static char *stack_top; | ||
#if MICROPY_ENABLE_GC | ||
static char heap[2048*16]; | ||
#endif | ||
|
||
extern void uart_init_ppc(int qemu); | ||
|
||
int main(int argc, char **argv) { | ||
int stack_dummy; | ||
stack_top = (char*)&stack_dummy; | ||
|
||
/* | ||
* microwatt has argc/r3 = 0 whereas QEMU has r3 set in head.S | ||
*/ | ||
uart_init_ppc(argc); | ||
// printf("Hello World\n"); | ||
|
||
#if MICROPY_ENABLE_PYSTACK | ||
static mp_obj_t pystack[1024]; | ||
mp_pystack_init(pystack, &pystack[1024]); | ||
#endif | ||
|
||
#if MICROPY_STACK_CHECK | ||
mp_stack_ctrl_init(); | ||
mp_stack_set_limit(48*1024); | ||
#endif | ||
|
||
#if MICROPY_ENABLE_GC | ||
gc_init(heap, heap + sizeof(heap)); | ||
#endif | ||
mp_init(); | ||
#if MICROPY_ENABLE_COMPILER | ||
#if MICROPY_REPL_EVENT_DRIVEN | ||
pyexec_event_repl_init(); | ||
for (;;) { | ||
int c = mp_hal_stdin_rx_chr(); | ||
if (pyexec_event_repl_process_char(c)) { | ||
break; | ||
} | ||
} | ||
#else | ||
pyexec_friendly_repl(); | ||
#endif | ||
#else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this commented out code be removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll remove. This was left over from the ports/minimal copy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove |
||
pyexec_frozen_module("frozentest.py"); | ||
#endif | ||
mp_deinit(); | ||
return 0; | ||
} | ||
|
||
void gc_collect(void) { | ||
// WARNING: This gc_collect implementation doesn't try to get root | ||
// pointers from CPU registers, and thus may function incorrectly. | ||
void *dummy; | ||
gc_collect_start(); | ||
gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t)); | ||
gc_collect_end(); | ||
gc_dump_info(); | ||
} | ||
|
||
mp_lexer_t *mp_lexer_new_from_file(const char *filename) { | ||
mp_raise_OSError(MP_ENOENT); | ||
} | ||
|
||
mp_import_stat_t mp_import_stat(const char *path) { | ||
return MP_IMPORT_STAT_NO_EXIST; | ||
} | ||
|
||
mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { | ||
return mp_const_none; | ||
} | ||
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); | ||
|
||
void nlr_jump_fail(void *val) { | ||
while (1); | ||
} | ||
|
||
void NORETURN __fatal_error(const char *msg) { | ||
while (1); | ||
} | ||
|
||
#ifndef NDEBUG | ||
void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) { | ||
printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); | ||
__fatal_error("Assertion failed"); | ||
} | ||
#endif | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"... MicroPython port to the PowerPC architecture that runs ..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops thanks :-)