Page 1 of 1

Serial doesn't work as expected after exiting boot services

Posted: Tue Dec 28, 2021 12:16 am
by sherbst
Recently got back into trying to implement a small OS for fun. It currently is simply a UEFI application/loader. I hope to make it function like some sort of loader to load my kernel.
When the loader starts up, I have it print some text over COM1. Redirecting serial to stdio with QEMU, I can see that this works. Indeed, after exiting boot services, writing to COM1 still works... sometimes.
I am writing this in Rust, and when attempt to send a simple string ("Hello, world\n") over COM1, it works fine. However, when I do some processing to generate this string (&format!("some value: {}", value)), nothing gets sent to COM1.

I have a similar issue with the UEFI graphics output protocol's frame buffer. I can write to the frame buffer before and after exiting boot services, but if the write to the frame buffer is behind a couple function calls, it just doesn't work.

In my small amount of understanding I thought maybe the stack was somehow overwriting these parts of memory. Not sure if that is happening or even possible, but I tried to inspect ebp and esp to see if anything was obvious. I find that for the entirety of the execution of my loader esp is 0x7FF1A130 and ebp is 0x0. This is further confusing to me because I thought ebp had to be at a higher address than esp. In addition, esp doesn't seem to move during the execution of the loader: another thing I would expect to happen. I also tried to move the stack to an arbitrary location, which gave me a page fault (not surprised).

All in all, not sure if the stack thing is the issue just thought I would mention it (and maybe someone can fill in my understanding on how the stack works).
Any ideas appreciated.

Thanks,
Sawyer

Re: Serial doesn't work as expected after exiting boot servi

Posted: Wed Dec 29, 2021 7:16 pm
by Octocontrabass
(This question was also posted elsewhere.)

According to the replies you got elsewhere, the problem might be related to memory allocation. Do you have a global allocator implementation? Does it rely on UEFI boot services?

Re: Serial doesn't work as expected after exiting boot servi

Posted: Sun Jan 02, 2022 6:00 pm
by deadmutex
sherbst wrote:I am writing this in Rust, and when attempt to send a simple string ("Hello, world\n") over COM1, it works fine. However, when I do some processing to generate this string (&format!("some value: {}", value)), nothing gets sent to COM1.
Something similar happened to me when I tried to used Rust to write a memory server. It turned out that liballoc was trying to allocate memory for the String, but my memory allocator wasn't initialized yet, so the code would panic. And then the panic handler would panic when it would try to print the panic message, resulting in an infinite loop. I chose to use byte slices (&[u8]) instead and implement the Write trait so that the write! macro can output the formatted sequence to the slice.