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

Better boot.S and link script #10

Merged
merged 2 commits into from Nov 21, 2021
Merged

Better boot.S and link script #10

merged 2 commits into from Nov 21, 2021

Conversation

dinosaure
Copy link
Owner

/cc @TheLortex may be a nicer boot.S and link script.
However, a bug persists. I think this layout solve the issue about bss nicely.
The remaining bug is about the Sys.argv.(0) and its usage. Indeed, a main.ml such as:

let () = Format.printf "Hello %s!" Sys.argv.(0)

But this code does not show anything:

let () = Format.printf "Hello %S!" Sys.argv.(0)

@dinosaure
Copy link
Owner Author

To go further, this code only shows Hello :

let () =
  Format.printf "Hello %!" ;
  Format.printf "%S%!" Sys.argv.(0) ;
  Format.printf "!\n%!"

The problem is definitely about the usage of Sys.argv.(0).

@dinosaure
Copy link
Owner Author

It seems that string_to_caml_string does something wrong.

@dinosaure dinosaure added the help wanted Extra attention is needed label Nov 21, 2021
@dinosaure
Copy link
Owner Author

I bisected where the Raspberry Pi 4 fails and it seems that:

String.unsafe_blit str 0 res 1 len ;

fails (only on Raspberry Pi 4). I'm not sure why but I will try to continue to introspect what is going on - note that I tried this code:

let string_to_caml_string str =
  let str = String.escaped str in
  let l = String.length str in
  let res = Bytes.make (l + 2) '\"' in
  Bytes.blit_string str 0 res 1 l;
  Bytes.unsafe_to_string res

let fn0 str =
  let _str = String.escaped str in
  Format.printf "[+][+] 000\n%!" ;
;;

let fn1 str =
  let str = String.escaped str in
  Format.printf "[+][+] 000\n%!" ;
  let len = String.length str in
  Format.printf "[+][+] 001\n%!" ;
  let _res = Bytes.make (len + 2) '\"' in
  Format.printf "[+][+] 002\n%!" ;
;;

let fn2 str =
  let str = String.escaped str in
  Format.printf "[+][+] 000\n%!" ;
  let len = String.length str in
  Format.printf "[+][+] 001\n%!" ;
  let res = Bytes.make (len + 2) '\"' in
  Format.printf "[+][+] 002\n%!" ;
  Bytes.blit_string str 0 res 1 len ;
  Format.printf "[+][+] 003\n%!" ;
;;

let () =
  Format.printf "[+] 001\n%!" ;
  let _res = Bytes.create 0x100 in
  Format.printf "[+] 002\n%!" ;
  let _str = String.escaped "\000\001\000" in
  Format.printf "[+] 003\n%!" ;
  let _str = String.length _str in
  Format.printf "[+] 004\n%!" ;
  let _str = "Hello World!" in
  Format.printf "[+] 005\n%!" ;
  let _len = String.length _str in
  Format.printf "[+] 006\n%!" ;
  Bytes.blit_string _str 0 (Bytes.create 0x100) 0 _len ;
  Format.printf "[+] 007\n%!" ;
  fn0 "gi(l)braltar" ;
  Format.printf "[+] 008\n%!" ;
  let _res = Bytes.make (_len + 2) '\"' in
  Format.printf "[+] 009\n%!" ;
  Bytes.blit Bytes.empty 0 _res 0 0 ;
  Format.printf "[+] 010\n%!" ;
  fn1 "gi(l)braltar" ;
  Format.printf "[+] 011\n%!" ;
  fn2 "gi(l)braltar" ;
  Format.printf "[+] 012\n%!" ;
  Format.printf ">>> %s.\n%!" (string_to_caml_string "gi(l)braltar") ;
;;

And the output is:

 _____ _ _ _           _ _           
|   __|_| | |_ ___ ___| | |_ ___ ___ 
|  |  | | | . |  _| .'| |  _| .'|  _|
|_____|_|_|___|_| |__,|_|_| |__,|_|  
RPi4: Memory map: 768 MB addressable:
RPi4:   reserved @ (0x0 - 0x7ffff)
RPi4:       text @ (0x80000 - 0xe91ef)
RPi4:     rodata @ (0xe91f0 - 0xf091f)
RPi4:       data @ (0xf0920 - 0x13970f)
RPi4:       heap >= 0x13a000 < stack < 0x30000000
[+] 001
[+] 002
[+] 003
[+] 004
[+] 005
[+] 006
[+] 007
[+][+] 000
[+] 008
[+] 009
[+] 010
[+][+] 000
[+][+] 001
[+][+] 002
[+] 011
[+][+] 000
[+][+] 001
[+][+] 002

@dinosaure
Copy link
Owner Author

The bug is about the implementation of memcpy, see mirage/ocaml-solo5#100 for more details. I will propose on this PR a simpler implementation of memcpy/memmove (so we will start to diverge from ocaml-freestanding/nolibc).

@dinosaure dinosaure merged commit b953611 into main Nov 21, 2021
@dinosaure dinosaure deleted the better-boot-S branch December 3, 2021 13:50
for (; n; n--) *d++ = *s++;
return dest;
for (; len; len--) *dst++ = *src++;
return dst;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the man page, the memcpy function should return a pointer to the dest address (return v_dst;). The returned value does not appear to be used here but at least mirage/mirage-crypto@f351442 uses it to ensure aligned memory usage and it may fail there. This is the same for memmove below.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will propose a PR to fix that, thanks for this looking.

@palainp
Copy link

palainp commented Jan 15, 2022

And btw, there may be some patch that has corrected the pb after this merge because I am currently able to run the example code completely with the original memcpy and memmove functions.

 _____ _ _ _           _ _                                                                                  
|   __|_| | |_ ___ ___| | |_ ___ ___                                                                        
|  |  | | | . |  _| .'| |  _| .'|  _|                                                                       
|_____|_|_|___|_| |__,|_|_| |__,|_|                                                                         
EL:1                                                                                                        
Interrupts: up                                                                                              
MMU: ON                                                                                                     
RPi4: Memory map: 752 MB addressable:                                                                       
RPi4:   reserved @ (0x0 - 0xffffffffffffffff)                                                                              
RPi4:       text @ (0x0 - 0x6518f)                                                                          
RPi4:     rodata @ (0x65190 - 0x6b6bf)                                                                      
RPi4:       data @ (0x6b6c0 - 0x1030c3f)                                                                    
RPi4:       heap >= 0x1031000 < stack < 0x2f000000                                                          
[+] 001                                                                                                     
[+] 002                                                                                                     
[+] 003                                                                                                     
[+] 004                                                                                                     
[+] 005                                                                                                     
[+] 006                                                                                                     
[+] 007                                                                                                     
[+][+] 000                                                                                                  
[+] 008                                                                                                     
[+] 009                                                                                                     
[+] 010                                                                                                     
[+][+] 000                                                                                                  
[+][+] 001                                                                                                  
[+][+] 002                                                                                                  
[+] 011                                                                                                     
[+][+] 000                                                                                                  
[+][+] 001                                                                                                  
[+][+] 002                                                                                                  
[+][+] 003                                                                                                  
[+] 012                                                                                                     
>>> "gi(l)braltar".                                                                                         

Maybe it is better to keep the same code here as the nolibc in ocaml-freestanding? The memcpy seems to really take care of 32b alignement and permit to copy 4B at a time which can be faster than 1B copy for longs arrays.

@dinosaure
Copy link
Owner Author

Hmhmm can you open a new issue for that to keep our experiments 👍

@dinosaure dinosaure mentioned this pull request Jan 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants