diff mbox

[1/3] kvm-unit-tests: add x86 port io accessors

Message ID 1298584085-13129-2-git-send-email-aliguori@us.ibm.com
State New
Headers show

Commit Message

Anthony Liguori Feb. 24, 2011, 9:48 p.m. UTC
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

Comments

Avi Kivity Feb. 27, 2011, 12:44 p.m. UTC | #1
On 02/24/2011 11:48 PM, Anthony Liguori wrote:
> Signed-off-by: Anthony Liguori<aliguori@us.ibm.com>
>
> diff --git a/lib/x86/io.h b/lib/x86/io.h
> new file mode 100644
> index 0000000..bd6341c
> --- /dev/null
> +++ b/lib/x86/io.h
> @@ -0,0 +1,40 @@
> +#ifndef IO_H
> +#define IO_H
> +
> +static inline unsigned char inb(unsigned short port)
> +{
> +    unsigned char value;
> +    asm volatile("inb %w1, %0" : "=a" (value) : "Nd" (port));
> +    return value;
> +}

Are those %[wb] really needed?  gcc should do the right thing based on 
the argument type.

Might as well put all of that into processor.h.
Anthony Liguori Feb. 27, 2011, 2:01 p.m. UTC | #2
On 02/27/2011 06:44 AM, Avi Kivity wrote:
> On 02/24/2011 11:48 PM, Anthony Liguori wrote:
>> Signed-off-by: Anthony Liguori<aliguori@us.ibm.com>
>>
>> diff --git a/lib/x86/io.h b/lib/x86/io.h
>> new file mode 100644
>> index 0000000..bd6341c
>> --- /dev/null
>> +++ b/lib/x86/io.h
>> @@ -0,0 +1,40 @@
>> +#ifndef IO_H
>> +#define IO_H
>> +
>> +static inline unsigned char inb(unsigned short port)
>> +{
>> +    unsigned char value;
>> +    asm volatile("inb %w1, %0" : "=a" (value) : "Nd" (port));
>> +    return value;
>> +}
>
> Are those %[wb] really needed?  gcc should do the right thing based on 
> the argument type.

It's just a little extra type safety.

>
> Might as well put all of that into processor.h.

Seems reasonable.

Regards,

Anthony Liguori
Avi Kivity Feb. 27, 2011, 3:32 p.m. UTC | #3
On 02/27/2011 04:01 PM, Anthony Liguori wrote:
> On 02/27/2011 06:44 AM, Avi Kivity wrote:
>> On 02/24/2011 11:48 PM, Anthony Liguori wrote:
>>> Signed-off-by: Anthony Liguori<aliguori@us.ibm.com>
>>>
>>> diff --git a/lib/x86/io.h b/lib/x86/io.h
>>> new file mode 100644
>>> index 0000000..bd6341c
>>> --- /dev/null
>>> +++ b/lib/x86/io.h
>>> @@ -0,0 +1,40 @@
>>> +#ifndef IO_H
>>> +#define IO_H
>>> +
>>> +static inline unsigned char inb(unsigned short port)
>>> +{
>>> +    unsigned char value;
>>> +    asm volatile("inb %w1, %0" : "=a" (value) : "Nd" (port));
>>> +    return value;
>>> +}
>>
>> Are those %[wb] really needed?  gcc should do the right thing based 
>> on the argument type.
>
> It's just a little extra type safety.

Yeah, but those constraints aren't really documented.  Linux does use 
them in a handful of places so they're unlikely to go away though.
diff mbox

Patch

diff --git a/lib/x86/io.h b/lib/x86/io.h
new file mode 100644
index 0000000..bd6341c
--- /dev/null
+++ b/lib/x86/io.h
@@ -0,0 +1,40 @@ 
+#ifndef IO_H
+#define IO_H
+
+static inline unsigned char inb(unsigned short port)
+{
+    unsigned char value;
+    asm volatile("inb %w1, %0" : "=a" (value) : "Nd" (port));
+    return value;
+}
+
+static inline unsigned short inw(unsigned short port)
+{
+    unsigned short value;
+    asm volatile("inw %w1, %0" : "=a" (value) : "Nd" (port));
+    return value;
+}
+
+static inline unsigned int inl(unsigned short port)
+{
+    unsigned int value;
+    asm volatile("inl %w1, %0" : "=a" (value) : "Nd" (port));
+    return value;
+}
+
+static inline void outb(unsigned char value, unsigned short port)
+{
+    asm volatile("outb %b0, %w1" : : "a"(value), "Nd"(port));
+}
+
+static inline void outw(unsigned short value, unsigned short port)
+{
+    asm volatile("outw %w0, %w1" : : "a"(value), "Nd"(port));
+}
+
+static inline void outl(unsigned int value, unsigned short port)
+{
+    asm volatile("outl %0, %w1" : : "a"(value), "Nd"(port));
+}
+
+#endif