Integrated debugger inside kernel

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Bonfra
Member
Member
Posts: 270
Joined: Wed Feb 19, 2020 1:08 pm
Libera.chat IRC: Bonfra
Location: Italy

Integrated debugger inside kernel

Post by Bonfra »

I'm having some troubles making my os work on real hardware so to avoid recompiling every time I change something to test and burning to the physical disk I took some inspiration from bzt's minidbg and implemented something like that in my kernel. It works flawlessly on QEMU with named pipes but I can't make it work with a real computer. I have an rs232-USB cable which is plugged by the serial port in the computer with my os on, and the USB part is in my other pc. I tried with minicom but nothing happens and the port is "offline"; using cat on the port to read just sits there forever and echoing to the port does nothing. For some obscure reason once I achieved to issue a command to the debugger with echo but then nothing. What am I missing?
Regards, Bonfra.
User avatar
pvc
Member
Member
Posts: 201
Joined: Mon Jan 15, 2018 2:27 pm

Re: Integrated debugger inside kernel

Post by pvc »

The old trick to debug problems with RS-232 connection problems is to short RxD and TxD lines (pin 2 and 3 on standard connectors) together and see if it echoes sent characters back.
User avatar
Bonfra
Member
Member
Posts: 270
Joined: Wed Feb 19, 2020 1:08 pm
Libera.chat IRC: Bonfra
Location: Italy

Re: Integrated debugger inside kernel

Post by Bonfra »

pvc wrote:The old trick to debug problems with RS-232 connection problems is to short RxD and TxD lines (pin 2 and 3 on standard connectors) together and see if it echoes sent characters back.
Do I need to connect together those two pins on their own or does the cable need to be plugged to the other end? it's a bit trivial to shorten them while it's connected. I tried to connect just these two pins without plugging the cable and nothing is sent back, maybe I need to do something else with the other pins?
Regards, Bonfra.
vvaltchev
Member
Member
Posts: 274
Joined: Fri May 11, 2018 6:51 am

Re: Integrated debugger inside kernel

Post by vvaltchev »

Are you sure you’re accessing the real serial port on hardware? Keep in mind that legacy COM1 interface and similars on I/O port 0x3f8 etc. are not available on modern hardware, even if you have a built in serial controller. You’ll have to discover it on boot, and very likely use memory mapped IO.
Tilck, a Tiny Linux-Compatible Kernel: https://github.com/vvaltchev/tilck
User avatar
Bonfra
Member
Member
Posts: 270
Joined: Wed Feb 19, 2020 1:08 pm
Libera.chat IRC: Bonfra
Location: Italy

Re: Integrated debugger inside kernel

Post by Bonfra »

vvaltchev wrote:Are you sure you’re accessing the real serial port on hardware?
I'm connecting the cable to this port here:
Image
vvaltchev wrote: Keep in mind that legacy COM1 interface and similars on I/O port 0x3f8 etc. are not available on modern hardware, even if you have a built in serial controller.
It's not really new; it's an optiplex 780 from 2010.
vvaltchev wrote:You’ll have to discover it on boot, and very likely use memory mapped IO.
So I need to call some bios interrupt to get this information? which one?
Regards, Bonfra.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Integrated debugger inside kernel

Post by nexos »

Thats the same PC I use :) . It should have ISA serial ports in it. Only modern UEFI machines don't have thoose. Note that to find what port the serial port is at, you should check the BDA.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
Ethin
Member
Member
Posts: 625
Joined: Sun Jun 23, 2019 5:36 pm
Location: North Dakota, United States

Re: Integrated debugger inside kernel

Post by Ethin »

I'd love to integrate a kernel debugger into my kernel but BZT's debugger isn't portable to other languages. So I'm stuck there and have no idea how to actually "write" a debugger. :)
What's the process for discovering UARTs on modern machines? Would they just be PCI devices?
Sorry to go OT but if I could get something like this working it'd greatly speed up my dev process.
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: Integrated debugger inside kernel

Post by Korona »

GDB has a standardized protocol (see the GDB documentation and our kernel implementation, it is not too complicated but a bit arcane). Since you're writing a kernel in Rust, you could make use of this crate (among others) which implements the GDB protocol (well, you still have to add your own hooks).

For discovering the UART: you can detect it via ACPI (that's the portable method), or hard code its PIO addresses (with a way for users to override these).
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
vvaltchev
Member
Member
Posts: 274
Joined: Fri May 11, 2018 6:51 am

Re: Integrated debugger inside kernel

Post by vvaltchev »

Bonfra wrote:
vvaltchev wrote:Are you sure you’re accessing the real serial port on hardware?
I'm connecting the cable to this port here:
I have no doubt you're connecting the cable in the right place. I was talking from the kernel point, if you're really accessing the controller that you're connecting. Legacy I/O ports like 0x3f8 might look like they're working, but in reality there's nothing on the HW side.
Bonfra wrote:It's not really new; it's an optiplex 780 from 2010.
2010 means "super new". When it's about x86, with "modern" hardware kernel developers here usually mean anything after year 2000, that supports ACPI etc. In order use the 0x3f8 I/O port, you need a "legacy PC" machine, probably from mid '90s or older.
Bonfra wrote:
vvaltchev wrote:You’ll have to discover it on boot, and very likely use memory mapped IO.
So I need to call some bios interrupt to get this information? which one?
As Korona said, you'd need to discover it via ACPI. It's a ton of work to get there. The quickest shortcut is to find UART's controller physical memory address using another operating system like Linux and (temporarily) hard-coding it in your project. I know it's far from great but.. "modern" hardware is complex to manage.
Tilck, a Tiny Linux-Compatible Kernel: https://github.com/vvaltchev/tilck
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: Integrated debugger inside kernel

Post by Korona »

@vvaltchev: Do you have physical hardware where 0x3F8 does not work? On the machines that I tested on (even newer ones), COM1 is still at that port (but COM 2, 3, 4 might not be at their legacy addresses - I have not checked that).
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
User avatar
Bonfra
Member
Member
Posts: 270
Joined: Wed Feb 19, 2020 1:08 pm
Libera.chat IRC: Bonfra
Location: Italy

Re: Integrated debugger inside kernel

Post by Bonfra »

vvaltchev wrote: As Korona said, you'd need to discover it via ACPI. It's a ton of work to get there. The quickest shortcut is to find UART's controller physical memory address using another operating system like Linux and (temporarily) hard-coding it in your project. I know it's far from great but.. "modern" hardware is complex to manage.
ACPI is a thing I didn't even consider since now, really hard in my opinion. Just to make it work I think I'm going to locate the address with Linux as you suggest. What is the command for doing this :) ?
Regards, Bonfra.
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Integrated debugger inside kernel

Post by Octocontrabass »

Open the BIOS setup and check the serial port configuration there. You might even be able to change which I/O port and IRQ it's assigned to.
User avatar
Bonfra
Member
Member
Posts: 270
Joined: Wed Feb 19, 2020 1:08 pm
Libera.chat IRC: Bonfra
Location: Italy

Re: Integrated debugger inside kernel

Post by Bonfra »

Octocontrabass wrote:Open the BIOS setup and check the serial port configuration there. You might even be able to change which I/O port and IRQ it's assigned to.
This is the only thing about serial ports, it's already set on COM1 so this should be right.
Image
Regards, Bonfra.
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Integrated debugger inside kernel

Post by Octocontrabass »

Well, there you go. Your serial port is using I/O port 0x3F8 and (ISA) IRQ 4.

If the serial port doesn't echo back the data you send when you short pins 2 and 3 together (with nothing else connected), the UART isn't programmed correctly.
User avatar
Bonfra
Member
Member
Posts: 270
Joined: Wed Feb 19, 2020 1:08 pm
Libera.chat IRC: Bonfra
Location: Italy

Re: Integrated debugger inside kernel

Post by Bonfra »

I swear I didn't touch anything in the code but now from the other pc `echo "c" > /dev/ttyUSB0` to the serial cable now works and send the "c" command (in my case continue execution) so the os continues normally. But nothing is received. Like when the OS boots up it should print `integrated debugger initialized` and every command should send an acknowledge back. But nothing is cought by `cat < /dev/ttyUSB0`
Regards, Bonfra.
Post Reply