MainframeReboot commented on issue #12356:
URL: https://github.com/apache/nuttx/issues/12356#issuecomment-2125321267

   Thank you all so much for your help. The recent suggestions pointed me down 
the right path and I can confirm that init now loads:
   
   
![image](https://github.com/apache/nuttx/assets/168458700/858aa881-fdd9-4519-825e-e2421076ea86)
   
   To get this to work, I took a look at the crt0.c source file mentioned by 
@patacongo as well as the linker scripts in the ../apps folder. I noticed that 
the entry point in the apps linker files does include crt0.o during linking but 
the entry point `__start` does not match the NuttX function in crt0.c. Within 
NuttX this function appears to have been changed to a single underscore which 
is why it could not be found.
   
   I also took the advice from @lupyuen and looked at the thread local storage 
configuration. As mentioned in my previous reply this wasn't set at all, so I 
added `CONFIG_TLS_ALIGNED=y` as well as `CONFIG_TLS_LOG2_MAXSTACK=16` into my 
.config file. I also increased the stack size to 64kb by setting 
`CONFIG_POSIX_SPAWN_DEFAULT_STACKSIZE=65536` as per his instructions
   
   @acassis, As I don't yet have a blog, I will document the other changes I 
had to make as the knsh defconfig for the mpfs icicle kit required additional 
modifications in order to work (not in chronological order):
   
   - The default flow of the Icicle kit utilizes Hart-Software-Services (HSS) 
on the E54 to boot and monitor system applications. The HSS configures the PMP 
so to ensure the PMP was fully unlocked and not causing issues, I modified the 
`HSS_PMP_Init` function from the original to one that contains only two entries 
(credit goes to @pussuw for this suggestion):
   ``` C
       csr_write(CSR_PMPADDR0, -1);
       csr_write(CSR_PMPCFG0, (PMP_A_NAPOT | PMP_R | PMP_W | PMP_X));
   ```
   - I noticed that `CONFIG_NUTTSBI` was set in the defconfig so payloads 
generated using the hss-payload-generator had the `skip-opensbi` flag set to 
true. More on the HSS file used below.
   - Out of the box, `CONFIG_RAM_VSTART` is set to 0x00. This caused issued 
inside of `riscv_pgvaddr()` during system initialization as the computed 
addresses would subtract `CONFIG_RAM_START` from the passed in address and add 
`CONFIG_RAM_VSTART` which was 0x00. This resulted in a hard fault as addresses 
outside of memory were being indexed.
   - Address environments were enabled but only `CONFIG_ARCH_DATA_VBASE` was 
set and the NPAGES was set to 0 for all. I modified this as follows:
   ```
   CONFIG_ARCH_TEXT_VBASE=0xC0000000
   CONFIG_ARCH_DATA_VBASE=0xC0400000
   CONFIG_ARCH_HEAP_VBASE=0xC0800000
   CONFIG_ARCH_TEXT_NPAGES=128
   CONFIG_ARCH_DATA_NPAGES=128
   CONFIG_ARCH_HEAP_NPAGES=128
   ```
   - Enabled Thread Local Storage by appending the following to the config file:
   ```
   CONFIG_TLS_ALIGNED=y
   CONFIG_TLS_LOG2_MAXSTACK=16
   CONFIG_TLS_NELEM=0
   ```
   - Next, I had to build the apps by first building NuttX, exporting it, and 
then importing it into the ../apps directory so I could build the apps and 
generate a `boot_romfsimg.h` header file. I followed the steps from this 
article to accomplish this: 
https://cwiki.apache.org/confluence/display/NUTTX/APPNAME+vs.+PROGNAME
   - I was getting strange warnings regarding `__start` not being found during 
linking which caused problems when running the apps so as per the suggestions 
from @patacongo, I took a look at `crt0.c` as well the make files in the /apps 
folder. Here I noticed that the /apps makefiles set the entry point as 
`__start` where as the actual function is named `_start` within `crt0.c`. I 
modified the apps linker script to remove the extra underscore which resolved 
the linker warnings.
   - I noticed the output was very large so I followed the article linked by 
@lupyuen to switch from relocatable ELF files to fully linked ELF files: 
https://github.com/lupyuen/quickjs-nuttx#full-linking-for-nuttx-apps
   - The files were still too large for my liking, so I used objcopy to strip 
the ELF files the `make import` generates in /apps/bin:
   ``` Bash
   riscv-none-elf-objcopy --strip-unneeded init
   riscv-none-elf-objcopy --strip-unneeded hello
   riscv-none-elf-objcopy --strip-unneeded helloxx
   riscv-none-elf-objcopy --strip-unneeded sh
   ```
   - After stripping the ELF files, I generated a new `boot_romfsimg.h` header 
file that was much smaller and copied it over to 
`nuttx/boards/risc-v/mpfs/icicle/include` and rebuilt NuttX.
   - Generated a payload using the `hss-payload-generator` using a modified 
yaml file to ensure the Kernel was running in Supervisor mode and that opensbi 
was skipped (this is different from the yaml that comes with NuttX and works 
for flat builds):
   ``` YAML
   # HSS Payload Generator
   
   # First, we can optionally set a name for our image, otherwise one will be 
created dynamically
   set-name: 'PolarFire-SoC-HSS::nuttxbsp_kernel_payload'
   
   # Next, we'll define the entry point addresses for each hart, as follows:
   hart-entry-points: {u54_1: '0x80000000', u54_2: '0x80000000', u54_3: 
'0x80000000', u54_4: '0x80000000>
   #
   payloads:
    nuttxbsp.bin: {exec-addr: '0x80000000', owner-hart: u54_1, priv-mode: 
prv_s, skip-opensbi: true}
   ```
   After all of that, NuttX kernel build runs on the PolarFire Icicle kit. 
Although I am running into an issue getting `helloxx` to run. The app `hello` 
works but I get a segmentation fault when running `helloxx`:
   
   
![image](https://github.com/apache/nuttx/assets/168458700/69a00b42-c1d6-438e-9468-c062cc8c6a8a)
   
   I noticed `crt0.c` has preprocessor definitions for CXX so I will look into 
this as well other C++ related configuration options as I might be missing 
something on that front.
   
   Thank you again @patacongo, @lupyuen, @acassis and @pussuw for your support 
on getting this to work.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to