1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/**
** @file ops.h
**
** @author Warren R. Carithers
**
** @brief Inline escapes to assembly for efficiency
**
** Inspiration from:
** Martins Mozeiko, https://gist.github.com/mmozeiko/f68ad2546bd6ab953315
** MIT's xv6, https://github.com/mit-pdos/xv6-public
**
** Note: normally, GCC doesn't inline unless the optimization level is
** over 1. This can be forced by adding
**
** __attribute__((always_inline))
**
** after the parameter list on each declaration. This is enabled by
** defining the compile-time CPP symbol FORCE_INLINING.
*/
#ifndef OPS_H_
#define OPS_H_
#include <common.h>
#ifndef ASM_SRC
// control "forced" inlining
#ifdef FORCE_INLINING
#define OPSINLINED __attribute__((always_inline))
#else
#define OPSINLINED /* no-op */
#endif /* FORCE_INLINING */
/****************************
** Data movement
****************************/
/**
** Block move functions
**
** Variations: movsb(), movsl(), movsq()
**
** Description: Copy from source buffer to destination buffer
**
** @param dst Destination buffer
** @param src Source buffer
** @param len Byte count
*/
static inline void movsb(void *dst, const void *src, uint32_t len) OPSINLINED
{
__asm__ __volatile__("cld; rep movsb"
: "+D"(dst), "+S"(src), "+c"(len)
:
: "memory");
}
static inline void movsw(void *dst, const void *src, uint32_t len) OPSINLINED
{
__asm__ __volatile__("cld; rep movsw"
: "+D"(dst), "+S"(src), "+c"(len)
:
: "memory");
}
static inline void movsl(void *dst, const void *src, uint32_t len) OPSINLINED
{
__asm__ __volatile__("cld; rep movsl"
: "+D"(dst), "+S"(src), "+c"(len)
:
: "memory");
}
static inline void movsq(void *dst, const void *src, uint32_t len) OPSINLINED
{
__asm__ __volatile__("cld; rep movsq"
: "+D"(dst), "+S"(src), "+c"(len)
:
: "memory");
}
/**
** Block store functions
**
** Variations: stosb(), stosw(), stosl()
**
** Description: Store a specific value into destination buffer
**
** @param dst Destination buffer
** @param val Data to copy
** @param len Byte count
*/
static inline void stosb(void *dst, uint8_t val, uint32_t len) OPSINLINED
{
__asm__ __volatile__("cld; rep stosb"
: "=D"(dst), "=c"(len)
: "0"(dst), "1"(len), "a"(val)
: "memory", "cc");
}
static inline void stosw(void *dst, uint16_t val, uint32_t len) OPSINLINED
{
__asm__ __volatile__("cld; rep stos2"
: "=D"(dst), "=c"(len)
: "0"(dst), "1"(len), "a"(val)
: "memory", "cc");
}
static inline void stosl(void *dst, uint32_t val, uint32_t len) OPSINLINED
{
__asm__ __volatile__("cld; rep stosl"
: "=D"(dst), "=c"(len)
: "0"(dst), "1"(len), "a"(val)
: "memory", "cc");
}
/****************************
** Special register access
****************************/
/**
** Register read functions
**
** Variations: r_cr0(), r_cr2(), r_cr3(), r_cr4(), r_eflags(),
** r_ebp(), r_esp()
**
** Description: Reads the register indicated by its name
**
** @return Contents of the register
*/
static inline uint32_t r_cr0(void) OPSINLINED
{
uint32_t val;
__asm__ __volatile__("movl %%cr0,%0" : "=r"(val));
return val;
}
static inline uint32_t r_cr2(void) OPSINLINED
{
uint32_t val;
__asm__ __volatile__("movl %%cr2,%0" : "=r"(val));
return val;
}
static inline uint32_t r_cr3(void) OPSINLINED
{
uint32_t val;
__asm__ __volatile__("movl %%cr3,%0" : "=r"(val));
return val;
}
static inline uint32_t r_cr4(void) OPSINLINED
{
uint32_t val;
__asm__ __volatile__("movl %%cr4,%0" : "=r"(val));
return val;
}
static inline uint32_t r_eflags(void) OPSINLINED
{
uint32_t val;
__asm__ __volatile__("pushfl; popl %0" : "=r"(val));
return val;
}
static inline uint32_t r_ebp(void) OPSINLINED
{
uint32_t val;
__asm__ __volatile__("movl %%ebp,%0" : "=r"(val));
return val;
}
static inline uint32_t r_esp(void) OPSINLINED
{
uint32_t val;
__asm__ __volatile__("movl %%esp,%0" : "=r"(val));
return val;
}
/**
** Register write functions
**
** Variations: w_cr0(), w_cr2(), w_cr3(), w_cr4(), w_eflags()
**
** Description: Writes a value into the CR indicated by its name
*/
static inline void w_cr0(uint32_t val) OPSINLINED
{
__asm__ __volatile__("movl %0,%%cr0" : : "r"(val));
}
static inline void w_cr2(uint32_t val) OPSINLINED
{
__asm__ __volatile__("movl %0,%%cr2" : : "r"(val));
}
static inline void w_cr3(uint32_t val) OPSINLINED
{
__asm__ __volatile__("movl %0,%%cr3" : : "r"(val));
}
static inline void w_cr4(uint32_t val) OPSINLINED
{
__asm__ __volatile__("movl %0,%%cr4" : : "r"(val));
}
static inline void w_eflags(uint32_t eflags) OPSINLINED
{
__asm__ __volatile__("pushl %0; popfl" : : "r"(eflags));
}
/**
** Descriptor table load functions
**
** Variations: w_gdt(), w_idt()
**
** Description: Load an address into the specified processor register
**
** @param addr The value to be loaded into the register
*/
static inline void w_gdt(void *addr) OPSINLINED
{
__asm__ __volatile__("lgdt (%0)" : : "r"(addr));
}
static inline void w_idt(void *addr) OPSINLINED
{
__asm__ __volatile__("lidt (%0)" : : "r"(addr));
}
/**
** CPU ID access
**
** Description: Retrieve CPUID information
**
** @param op Value to be placed into %eax for the operation
** @param ap Pointer to where %eax contents should be saved, or NULL
** @param bp Pointer to where %ebx contents should be saved, or NULL
** @param cp Pointer to where %ecx contents should be saved, or NULL
** @param dp Pointer to where %edx contents should be saved, or NULL
*/
static inline void cpuid(uint32_t op, uint32_t *ap, uint32_t *bp, uint32_t *cp,
uint32_t *dp) OPSINLINED
{
uint32_t eax, ebx, ecx, edx;
__asm__ __volatile__("cpuid"
: "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
: "a"(op));
if (ap)
*ap = eax;
if (bp)
*bp = ebx;
if (cp)
*cp = ecx;
if (dp)
*dp = edx;
}
/****************************
** TLB management
****************************/
/**
** TLB invalidation for one page
**
** Description: Invalidate the TLB entry for an address
**
** @param addr An address within the page to be flushed
*/
static inline void invlpg(uint32_t addr) OPSINLINED
{
__asm__ __volatile__("invlpg (%0)" : : "r"(addr) : "memory");
}
/**
** TLB invalidation for all pages
**
** Description: Flush all entries from the TLB
**
** We do this by changing CR3.
*/
static inline void flushtlb(void) OPSINLINED
{
uint32_t cr3;
__asm__ __volatile__("movl %%cr3,%0" : "=r"(cr3));
__asm__ __volatile__("movl %0,%%cr2" : : "r"(cr3));
}
/****************************
** I/O instructions
****************************/
/**
** Name: inN
**
** Variations: inb(), inw(), inl()
**
** Description: Read some amount of data from the supplied I/O port
**
** @param port The i/o port to read from
**
** @return The data read from the specified port
*/
static inline uint8_t inb(int port) OPSINLINED
{
uint8_t data;
__asm__ __volatile__("inb %w1,%0" : "=a"(data) : "d"(port));
return data;
}
static inline uint16_t inw(int port) OPSINLINED
{
uint16_t data;
__asm__ __volatile__("inw %w1,%0" : "=a"(data) : "d"(port));
return data;
}
static inline uint32_t inl(int port) OPSINLINED
{
uint32_t data;
__asm__ __volatile__("inl %w1,%0" : "=a"(data) : "d"(port));
return data;
}
/**
** Name: outN
**
** Variations: outb(), outw(), outl()
**
** Description: Write some data to the specified I/O port
**
** @param port The i/o port to write to
** @param data The data to be written to the port
**
** @return The data read from the specified port
*/
static inline void outb(int port, uint8_t data) OPSINLINED
{
__asm__ __volatile__("outb %0,%w1" : : "a"(data), "d"(port));
}
static inline void outw(int port, uint16_t data) OPSINLINED
{
__asm__ __volatile__("outw %0,%w1" : : "a"(data), "d"(port));
}
static inline void outl(int port, uint32_t data) OPSINLINED
{
__asm__ __volatile__("outl %0,%w1" : : "a"(data), "d"(port));
}
/****************************
** Miscellaneous instructions
****************************/
/**
** Name: breakpoint
**
** Description: Cause a breakpoint interrupt for debugging purposes
*/
static inline void breakpoint(void) OPSINLINED
{
__asm__ __volatile__("int3");
}
/**
** Name: get_ra
**
** Description: Get the return address for the calling function
** (i.e., where whoever called us will go back to)
**
** @return The address the calling routine will return to as a uint32_t
*/
static inline uint32_t get_ra(void) OPSINLINED
{
uint32_t val;
__asm__ __volatile__("movl 4(%%ebp),%0" : "=r"(val));
return val;
}
/**
** Name: ev_wait
**
** Description: Pause until something happens
*/
static inline void ev_wait(void) OPSINLINED
{
__asm__ __volatile__("sti ; hlt");
}
/**
** Name: xchgl
**
** Description: Perform an atomic exchange with memory
**
** @param addr Memory location to be modified
** @param data Data to exchange
**
** @return The old contents of the memory location
*/
static inline uint32_t xchgl(volatile uint32_t *addr, uint32_t data) OPSINLINED
{
uint32_t old;
// + indicates a read-modify-write operand
__asm__ __volatile__("lock; xchgl %0, %1"
: "+m"(*addr), "=a"(old)
: "1"(data)
: "cc");
return old;
}
#endif /* !ASM_SRC */
#endif
|