Skip to content
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

[sw] add support for newlib's system calls #275

Merged
merged 11 commits into from
Feb 11, 2022
Merged

Conversation

stnolting
Copy link
Owner

@stnolting stnolting commented Feb 10, 2022

This PR add support for RISC-V's newlib system calls by providing simple stubs for "OS" functions like fork() and write() and even printf() and scanf(). A new library file sw/lib/source/syscalls.c is added to provide these stubs.

Furthermore, this PR updates the processor's linker script (sw/common/neorv32.ld) by providing definitions for setting up the heap. Application software can now use standard dynamic memory allocation functions like malloc() and free().


📚 A new section in the data sheet ("RAM Layout") illustrates the default RAM usage.

⚠️ The concepts from this PR are still experimental - even though all tests using functions provided by syscalls.c seem to work fine. Any feedback/help is highly appreciated!

©️ The stubs for the system calls were copied (and modified) from @openhwgroup's https://github.com/openhwgroup/cv32e40p/blob/master/example_tb/core/custom/syscalls.c (project license: SOLDERPAD HARDWARE LICENSE version 0.51) and @newaetech's newaetech/chipwhisperer@df8e2fa

Further references on handling newlib system calls: https://interrupt.memfault.com/blog/boostrapping-libc-with-newlib#implementing-newlib

@stnolting stnolting added the SW software-related label Feb 10, 2022
@stnolting stnolting self-assigned this Feb 10, 2022
@stnolting stnolting changed the title [sw] add support for newlib's system calls (stubs!) [sw] add support for newlib's system calls Feb 10, 2022
@stnolting stnolting added the experimental Experimental feature label Feb 10, 2022
@stnolting stnolting marked this pull request as ready for review February 11, 2022 06:44
@stnolting
Copy link
Owner Author

stnolting commented Feb 11, 2022

✔️🚀 A minimal test program using dynamic memory allocation and printf:

#include <neorv32.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int main() {

  // initialize the neorv32 runtime environment
  // this will take care of handling all CPU traps (interrupts and exceptions)
  neorv32_rte_setup();

  // setup UART0 at default baud rate, no parity bits, no hw flow control
  neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_RTSCTS);

  // output via "STDOUT" (UART0.TX)
  printf("printf test\n");

  // allocate 4 bytes of memory
  char *char_buffer;
  char_buffer = (char *) malloc(4 * sizeof(char));

  // get 4 chars from "STDIN" (UART0.RX)
  read((int)stdin, char_buffer, 4 * sizeof(char));

  // output via UART0.TX
  neorv32_uart0_printf("%c %c %c %c\n", char_buffer[0], char_buffer[1], char_buffer[2], char_buffer[3]);

  free(char_buffer);

  return 0;
}

And the resulting output (when entering 1, 2, 3, 4 via the UART console):

printf test
1 2 3 4

⚠️ Dynamic memory should be used with caution to prevent collisions with the stack! The stack starts at the end of the RAM (like DMEM) and grows downwards. The heap start at the end of the initialized and un-initialized data sections (.data and .bss) and grows upwards. The default maximum heap size is 1/4 of the RAM (DMEM). This definition can be altered by explicitly setting the __heap_size symbol.

⚠️ printf() and scanf() are quite powerful functions and require a lot of memory. Furthermore, they use UART0 only. It is highly recommended to use the NEORV32-optimized functions like neorv32_uart[0/1]_printf().

@stnolting stnolting merged commit 745d8db into main Feb 11, 2022
@stnolting stnolting deleted the sw_newlib_support branch February 11, 2022 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
experimental Experimental feature SW software-related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant