]> git.proxmox.com Git - mirror_edk2.git/blob - DuetPkg/CpuIoDxe/Ia32/CpuIoAccessGNU.c
Fix build issue in linux environment.
[mirror_edk2.git] / DuetPkg / CpuIoDxe / Ia32 / CpuIoAccessGNU.c
1 /* This file is only used when not able to compile the MASM CpuIoAccess.asm
2 NOTE: Compiling with -fomit-frame-pointer would get you to roughly the exact
3 same code as the MASM file although GCC will typically include movzbl %al, %eax
4 or movzwl %ax, %eax instructions on the read functions such that the entire
5 eax result register will be valid, not just the lowest 8 or 16 bits.
6 */
7 #ifdef __GNUC__
8
9 /* A quick note about GCC inline asm and the GNU assembler:
10 When gas encounters an instruction with a suffix (e.g. inb, inw, or inl vs. just in) it will
11 warn if the operand corresponding to the suffix is not of the correct size and will assume you
12 meant what you said when you specified the suffix.
13
14 Because GCC does not enable us to see whether it is replacing %0 with %al, %ax, or %eax it is
15 helpful to have the assembler warn us that GCC is making an incorrect assumption. The actual
16 in or out instruction will always be generated correctly in this case since the assembler is
17 correct in assuming we meant what we said when we specified the suffix. However, GCC might
18 generate incorrect surrounding code. For example, if we were to incorrectly specify the
19 output size of an in instruction as UINT32, GCC would potentially fail to issue movz(b|w)l after
20 it under the assumption that the in instruction filled the entire eax register and not just
21 the al or ax portion.
22
23 GCC determines which size of register to use based on the C data type. So for in instructions
24 the interesting type is that of the automatic variable named Data which is specified as an
25 output operand to the inline assembly statement. For example:
26
27 UINT8 Data;
28 asm ( "inb %1, %0"
29 : "=a"(Data)
30 : "d"(Port)
31 );
32 return Data;
33
34 In this case, GCC will replace %0 with %al. If Data had been specified as UINT16, it would replace
35 %0 with %ax, and for UINT32 with %eax.
36
37 Likewise in the case of IA32 out instructions, GCC will replace %0 with the appropriately sized
38 register based on the size of the input operand. There is one gotcha though. The CpuIoWrite
39 series of functions all use UINT32 as the type of the second (Data) argument. This means that
40 for GCC to output the correct register size we must cast it appropriately.
41
42 The Port number is always a UINT16 so GCC will always ouput %dx.
43 */
44
45 #include "CpuIoAccess.h"
46
47 UINT8
48 EFIAPI
49 CpuIoRead8 (
50 IN UINT16 Port
51 )
52 {
53 UINT8 Data;
54 asm ( "inb %1, %0"
55 : "=a"(Data)
56 : "d"(Port)
57 );
58 return Data;
59 }
60
61 UINT16
62 EFIAPI
63 CpuIoRead16 (
64 IN UINT16 Port
65 )
66 {
67 UINT16 Data;
68 asm ( "inw %1, %0"
69 : "=a"(Data)
70 : "d"(Port)
71 );
72 return Data;
73 }
74
75 UINT32
76 EFIAPI
77 CpuIoRead32 (
78 IN UINT16 Port
79 )
80 {
81 UINT32 Data;
82 asm ( "inl %1, %0"
83 : "=a"(Data)
84 : "d"(Port)
85 );
86 return Data;
87 }
88
89 VOID
90 EFIAPI
91 CpuIoWrite8 (
92 IN UINT16 Port,
93 IN UINT32 Data
94 )
95 {
96 asm ( "outb %1, %0"
97 : /* No outputs */
98 : "d"(Port)
99 , "a"((UINT8)Data)
100 );
101 }
102
103 VOID
104 EFIAPI
105 CpuIoWrite16 (
106 IN UINT16 Port,
107 IN UINT32 Data
108 )
109 {
110 asm ( "outw %1, %0"
111 : /* No outputs */
112 : "d"(Port)
113 , "a"((UINT16)Data)
114 );
115 }
116
117 VOID
118 EFIAPI
119 CpuIoWrite32 (
120 IN UINT16 Port,
121 IN UINT32 Data
122 )
123 {
124 asm ( "outl %1, %0"
125 : /* No outputs */
126 : "d"(Port)
127 /* NOTE: Cast is technically unnecessary but we use it to illustrate
128 that we always want to output a UINT32 and never anything else.
129 */
130 , "a"((UINT32)Data)
131 );
132 }
133
134 #endif /* def __GNUC__ */