OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Apr 23, 2024 2:46 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: Explanation of hardware communication
PostPosted: Sun Sep 23, 2018 4:15 am 
Offline

Joined: Sat Sep 22, 2018 2:38 pm
Posts: 18
Hello,

I found that you can "communicate" with hardware using Assembly in C like here: https://wiki.osdev.org/Inline_Assembly/Examples category I/O access.

As far as I understand, for example function outb will set some value at some port. First question what is that port? I know only ports in network, is that port something like address of specific hardware?

Where can I find all ports what can I use?


Top
 Profile  
 
 Post subject: Re: Explanation of hardware communication
PostPosted: Sun Sep 23, 2018 4:29 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
https://wiki.osdev.org/I/O_Ports


Top
 Profile  
 
 Post subject: Re: Explanation of hardware communication
PostPosted: Sun Sep 23, 2018 8:42 am 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 934
Location: USA
Be forewarned, writing incorrect values to an I/O port can do damage to the hardware it is attached to. You should really read up on the hardware you wish to program, the types of ports it has, Port I/O or Mem-mapped I/O, are the registers Write Only, Read Only, Write Clear, Write One, etc.

Find a good resource on the hardware you wish to write to before you start writing to the I/O port(s) in question.

Ben
- http://www.fysnet.net/osdesign_book_series.htm


Top
 Profile  
 
 Post subject: Re: Explanation of hardware communication
PostPosted: Sun Sep 23, 2018 12:16 pm 
Offline

Joined: Sat Sep 22, 2018 2:38 pm
Posts: 18
Oh really inserting values into ports can damage hardware?

So if I will learn I/O in Virtual Machine like VirtualBox then it is safe, right?


Can someone tell me what does this commands do?


Code:
__asm__ __volatile__ ("inb %1, %0" : "=a" (result) : "dN" (port));

__asm__ __volatile__ ("outb %1, %0" :: "dN" (port), "a" (value));


I know that inb is for getting values from port and outb is for putting values at specific port.

But what does that dN, a and =a mean?


Top
 Profile  
 
 Post subject: Re: Explanation of hardware communication
PostPosted: Sun Sep 23, 2018 2:09 pm 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 934
Location: USA
Quote:
Oh really inserting values into ports can damage hardware?

(That almost sounds sarcastic :-))

Using an emulator is a safe and usually best practice when doing things of this sort.

Quote:
But what does that dN, a and =a mean?

This is Compiler/Assembler specific. This is part of the compiler being used to compile the inline assembly code. This particular compiler uses that syntax to let the writer tell the compiler about the instruction and registers used.

Ben


Top
 Profile  
 
 Post subject: Re: Explanation of hardware communication
PostPosted: Sun Sep 23, 2018 2:38 pm 
Offline

Joined: Sat Sep 22, 2018 2:38 pm
Posts: 18
BenLunt wrote:
Quote:
Oh really inserting values into ports can damage hardware?

(That almost sounds sarcastic :-))

I didn't mean it as sarcastic :D

BenLunt wrote:
This is Compiler/Assembler specific. This is part of the compiler being used to compile the inline assembly code. This particular compiler uses that syntax to let the writer tell the compiler about the instruction and registers used.


Yes I know that it is inline Assembly in C, and I understand that it is used to communicate with hardware ports, but I don't understand that parameters like dN, a and =a.


Top
 Profile  
 
 Post subject: Re: Explanation of hardware communication
PostPosted: Sun Sep 23, 2018 2:46 pm 
Offline
Member
Member
User avatar

Joined: Sat Dec 27, 2014 9:11 am
Posts: 901
Location: Maadi, Cairo, Egypt
tomsk wrote:
Yes I know that it is inline Assembly in C, and I understand that it is used to communicate with hardware ports, but I don't understand that parameters like dN, a and =a.

That question is hardly relevant to OSDev. That is about GCC's inline assembly syntax, which if you want to use inline assembly, you need to know.

_________________
You know your OS is advanced when you stop using the Intel programming guide as a reference.


Top
 Profile  
 
 Post subject: Re: Explanation of hardware communication
PostPosted: Sun Sep 23, 2018 11:39 pm 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
You need to learn to use Google. You can find answers to your questions there; it is recommended that you do some research before posting questions here. Also, search the wiki.


Top
 Profile  
 
 Post subject: Re: Explanation of hardware communication
PostPosted: Mon Sep 24, 2018 12:20 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
@BenLunt: Could I ask you to expand on the statement that hardware can be damaged by writing to I/O ports?

I know that, at one time, careless programming of a video card could damage a CRT monitor, but I don't know of any other examples. It would be very useful to have a list of hardware that could be damaged in this way.


Top
 Profile  
 
 Post subject: Re: Explanation of hardware communication
PostPosted: Mon Sep 24, 2018 9:27 am 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 934
Location: USA
iansjack wrote:
@BenLunt: Could I ask you to expand on the statement that hardware can be damaged by writing to I/O ports?

I know that, at one time, careless programming of a video card could damage a CRT monitor, but I don't know of any other examples. It would be very useful to have a list of hardware that could be damaged in this way.

yes it would be useful to have a list like this. Let me start a list:

1. All of them
2. see 1

(smile)

There are few, if any, hardware devices that will watch for and catch harmful programming. That would be too expensive to implement. It is assumed that you as the programmer will read the documentation, understand the documentation, and write "unharmful" code.

Granted, writing bogus data to most hardware will simple place the hardware in an unknown state. However, there is other damage that can be done as well.

For example, the RTL8169 network card will dump its internal counters to a buffer you specify. If you write bogus data to the buffer register, then set the command byte to dump the internals, again by writing bogus data to that register, the card will write to the address specified. This address could be an address within your kernel's task management code, re-writing the task manager. What kind of damage could come of that?

The point is, to program hardware via direct access (Port I/O or mem-mapped I/O), you need to know what you are doing.

Ben


Top
 Profile  
 
 Post subject: Re: Explanation of hardware communication
PostPosted: Mon Sep 24, 2018 11:22 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

BenLunt wrote:
For example, the RTL8169 network card will dump its internal counters to a buffer you specify. If you write bogus data to the buffer register, then set the command byte to dump the internals, again by writing bogus data to that register, the card will write to the address specified. This address could be an address within your kernel's task management code, re-writing the task manager. What kind of damage could come of that?


How about, none? For this scenario the chance of permanent hardware damage is insignificant and the most likely end result is that the OS will crash.

Things that can damage hardware (that I know of) are:
  • Setting a "too high frequency" video mode (either correctly or incorrectly) can cause some ancient CRT monitors to "blow up" and never work again; which is about the only good thing you can do with these old CRT monitors.
  • On some cheap and nasty notebook/laptops; setting a dodgy video mode can cause the liquid crystals to overheat/boil and cause permanent damage to the screen.
  • For floppy drives, if you repeatedly slam the heads against the stops (e.g. by continually sending it to "track 128") you'll eventually break the drive (might take a several hours)
  • Under (typically incredibly unlikely) conditions it might be possible to "accidentally" overwrite the firmware's ROM and brick a computer.
  • If you enable "ACPI mode" (to disable firmware's SMM handler) and then fail to handle ACPI events (from thermal sensors, etc) properly, for some relatively rare CPUs where the thermal shutdown is buggy you might overheat the CPU badly enough to damage it.
  • For some chipsets (intended for "gamers") there's an incredibly small chance of "accidentally over-overclocking" the CPU, memory or GPU so badly that it causes damage (typically in a "higher risk of future failure" way).


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re: Explanation of hardware communication
PostPosted: Mon Sep 24, 2018 11:35 am 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 934
Location: USA
Brendan wrote:
BenLunt wrote:
For example, the RTL8169 network card will dump its internal counters to a buffer you specify. If you write bogus data to the buffer register, then set the command byte to dump the internals, again by writing bogus data to that register, the card will write to the address specified. This address could be an address within your kernel's task management code, re-writing the task manager. What kind of damage could come of that?

Hi Brendan,

Brendan wrote:
How about, none? For this scenario the chance of permanent hardware damage is insignificant and the most likely end result is that the OS will crash.

- Under (typically incredibly unlikely) conditions it might be possible to "accidentally" overwrite the firmware's ROM and brick a computer.

And that was my point exactly. If the (RTL8169) write is where the current task manager happens to be, this will destroy the task manager, disable task switching and do who-knows-what. Yes, most likely crash, but isn't this damage?

Anyway, I think the OP gets the idea.

Ben


Top
 Profile  
 
 Post subject: Re: Explanation of hardware communication
PostPosted: Mon Sep 24, 2018 9:01 pm 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

BenLunt wrote:
Brendan wrote:
How about, none? For this scenario the chance of permanent hardware damage is insignificant and the most likely end result is that the OS will crash.

- Under (typically incredibly unlikely) conditions it might be possible to "accidentally" overwrite the firmware's ROM and brick a computer.

And that was my point exactly. If the (RTL8169) write is where the current task manager happens to be, this will destroy the task manager, disable task switching and do who-knows-what. Yes, most likely crash, but isn't this damage?


"Damage" usually implies permanent hardware damage. "Crash" usually implies that software stops working but there's no permanent hardware damage and you can reset the computer and everything will be fine.

What you're really trying to do is discourage "trial and error software development" (prevent people from wasting their time trying silly things because they didn't read the datasheet or specification for a device and have no idea what they're doing), which is good (until you start trying to scare people stories about damage). The problem is that sometimes beginners will be too scared to test their code on real hardware because they've over-estimated the chance of causing permanent hardware damage (e.g. because they think that one trivial little bug anywhere will probably cost them $1234 to replace their laptop); where the reality is that being bitten by a shark while on dry land on a calm day is probably more likely accidentally causing permanent hardware damage.


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re: Explanation of hardware communication
PostPosted: Tue Sep 25, 2018 5:01 am 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 934
Location: USA
Brendan wrote:
Hi,
"Damage" usually implies permanent hardware damage. "Crash" usually implies that software stops working but there's no permanent hardware damage and you can reset the computer and everything will be fine.

What you're really trying to do is discourage "trial and error software development" (prevent people from wasting their time trying silly things because they didn't read the datasheet or specification for a device and have no idea what they're doing), which is good (until you start trying to scare people stories about damage). The problem is that sometimes beginners will be too scared to test their code on real hardware because they've over-estimated the chance of causing permanent hardware damage (e.g. because they think that one trivial little bug anywhere will probably cost them $1234 to replace their laptop); where the reality is that being bitten by a shark while on dry land on a calm day is probably more likely accidentally causing permanent hardware damage.

Yes indeed. My thoughts exactly, though you put it much better than I did.

Ben


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 117 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group