Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 1 | // Basic x86 asm functions and function defs. |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // |
| 5 | // This file may be distributed under the terms of the GNU GPLv3 license. |
Kevin O'Connor | 786502d | 2008-02-27 10:41:41 -0500 | [diff] [blame] | 6 | #ifndef __UTIL_H |
| 7 | #define __UTIL_H |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 8 | |
| 9 | #include "ioport.h" // outb |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 10 | #include "biosvar.h" // struct bregs |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 11 | |
Kevin O'Connor | 786502d | 2008-02-27 10:41:41 -0500 | [diff] [blame] | 12 | static inline void irq_disable(void) |
| 13 | { |
| 14 | asm volatile("cli": : :"memory"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 15 | } |
| 16 | |
Kevin O'Connor | 786502d | 2008-02-27 10:41:41 -0500 | [diff] [blame] | 17 | static inline void irq_enable(void) |
| 18 | { |
| 19 | asm volatile("sti": : :"memory"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | static inline unsigned long irq_save(void) |
| 23 | { |
| 24 | unsigned long flags; |
| 25 | asm volatile("pushfl ; popl %0" : "=g" (flags)); |
| 26 | irq_disable(); |
| 27 | return flags; |
| 28 | } |
| 29 | |
| 30 | static inline void irq_restore(unsigned long flags) |
| 31 | { |
| 32 | asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc"); |
| 33 | } |
| 34 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 35 | static inline void nop(void) |
| 36 | { |
| 37 | asm volatile("nop"); |
| 38 | } |
| 39 | |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 40 | static inline void hlt(void) |
| 41 | { |
| 42 | asm volatile("hlt"); |
| 43 | } |
| 44 | |
Kevin O'Connor | 8ce2cd8 | 2008-03-02 08:48:05 -0500 | [diff] [blame] | 45 | #define BX_PANIC(fmt, args...) bprintf(0, fmt , ##args) |
Kevin O'Connor | 4ce6a49 | 2008-02-29 00:21:27 -0500 | [diff] [blame] | 46 | #define BX_INFO(fmt, args...) bprintf(0, fmt , ##args) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 47 | |
| 48 | static inline void |
| 49 | memset(void *s, int c, size_t n) |
| 50 | { |
| 51 | while (n) |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 52 | ((char *)s)[--n] = c; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Kevin O'Connor | a4d3576 | 2008-03-08 15:43:03 -0500 | [diff] [blame] | 55 | static inline void * |
| 56 | memcpy(void *d1, const void *s1, size_t len) |
| 57 | { |
| 58 | u8 *d = d1; |
| 59 | const u8 *s = s1; |
| 60 | |
| 61 | while (len--) { |
| 62 | *d++ = *s++; |
| 63 | } |
| 64 | return d1; |
| 65 | } |
| 66 | |
Kevin O'Connor | 4b60c00 | 2008-02-25 22:29:55 -0500 | [diff] [blame] | 67 | static inline void |
| 68 | eoi_master_pic() |
| 69 | { |
| 70 | outb(PIC1_IRQ5, PORT_PIC1); |
| 71 | } |
| 72 | |
| 73 | static inline void |
| 74 | eoi_both_pics() |
| 75 | { |
| 76 | outb(PIC2_IRQ13, PORT_PIC2); |
| 77 | eoi_master_pic(); |
| 78 | } |
| 79 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 80 | static inline |
| 81 | void call16(struct bregs *callregs) |
| 82 | { |
| 83 | asm volatile( |
Kevin O'Connor | b8aacb0 | 2008-03-01 14:56:07 -0500 | [diff] [blame] | 84 | "pushl %%ebp\n" // Save state |
| 85 | "pushfl\n" |
Kevin O'Connor | 3a47a31 | 2008-03-01 14:46:37 -0500 | [diff] [blame] | 86 | #ifdef MODE16 |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 87 | "calll __call16\n" |
Kevin O'Connor | 3a47a31 | 2008-03-01 14:46:37 -0500 | [diff] [blame] | 88 | #else |
| 89 | "calll __call16_from32\n" |
| 90 | #endif |
Kevin O'Connor | b8aacb0 | 2008-03-01 14:56:07 -0500 | [diff] [blame] | 91 | "popfl\n" // Restore state |
| 92 | "popl %%ebp\n" |
Kevin O'Connor | 8ce2cd8 | 2008-03-02 08:48:05 -0500 | [diff] [blame] | 93 | : "+a" (callregs), "+m" (*callregs) |
| 94 | : |
Kevin O'Connor | b8aacb0 | 2008-03-01 14:56:07 -0500 | [diff] [blame] | 95 | : "ebx", "ecx", "edx", "esi", "edi"); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 96 | } |
| 97 | |
Kevin O'Connor | 3a47a31 | 2008-03-01 14:46:37 -0500 | [diff] [blame] | 98 | static inline |
| 99 | void __call16_int(struct bregs *callregs, u16 offset) |
| 100 | { |
Kevin O'Connor | 44c631d | 2008-03-02 11:24:36 -0500 | [diff] [blame] | 101 | callregs->cs = SEG_BIOS; |
Kevin O'Connor | 3a47a31 | 2008-03-01 14:46:37 -0500 | [diff] [blame] | 102 | callregs->ip = offset; |
| 103 | call16(callregs); |
| 104 | } |
| 105 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 106 | #ifdef MODE16 |
Kevin O'Connor | 3a47a31 | 2008-03-01 14:46:37 -0500 | [diff] [blame] | 107 | #define call16_int(nr, callregs) do { \ |
| 108 | extern void irq_trampoline_ ##nr (); \ |
| 109 | __call16_int((callregs), (u16)&irq_trampoline_ ##nr ); \ |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 110 | } while (0) |
| 111 | #else |
| 112 | #include "../out/rom16.offset.auto.h" |
Kevin O'Connor | 3a47a31 | 2008-03-01 14:46:37 -0500 | [diff] [blame] | 113 | #define call16_int(nr, callregs) \ |
| 114 | __call16_int((callregs), OFFSET_irq_trampoline_ ##nr ) |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 115 | #endif |
| 116 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 117 | // output.c |
| 118 | void bprintf(u16 action, const char *fmt, ...) |
| 119 | __attribute__ ((format (printf, 2, 3))); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 120 | void __debug_enter(const char *fname, struct bregs *regs); |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 121 | void __debug_fail(const char *fname, struct bregs *regs); |
Kevin O'Connor | 4ce6a49 | 2008-02-29 00:21:27 -0500 | [diff] [blame] | 122 | void __debug_stub(const char *fname, struct bregs *regs); |
Kevin O'Connor | c65a380 | 2008-03-02 13:58:23 -0500 | [diff] [blame] | 123 | void __debug_isr(const char *fname, struct bregs *regs); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 124 | #define debug_enter(regs) \ |
| 125 | __debug_enter(__func__, regs) |
Kevin O'Connor | 4ce6a49 | 2008-02-29 00:21:27 -0500 | [diff] [blame] | 126 | #define debug_stub(regs) \ |
| 127 | __debug_stub(__func__, regs) |
Kevin O'Connor | c65a380 | 2008-03-02 13:58:23 -0500 | [diff] [blame] | 128 | #define debug_isr(regs) \ |
| 129 | __debug_isr(__func__, regs) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 130 | #define printf(fmt, args...) \ |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 131 | bprintf(1, fmt , ##args ) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 132 | |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 133 | // Frequently used return codes |
| 134 | #define RET_EUNSUPPORTED 0x86 |
| 135 | static inline void |
| 136 | set_success(struct bregs *regs) |
| 137 | { |
| 138 | set_cf(regs, 0); |
| 139 | } |
| 140 | |
| 141 | static inline void |
| 142 | set_code_success(struct bregs *regs) |
| 143 | { |
| 144 | regs->ah = 0; |
| 145 | set_cf(regs, 0); |
| 146 | } |
| 147 | |
| 148 | #define set_fail(regs) do { \ |
| 149 | __debug_fail(__func__, (regs)); \ |
| 150 | set_cf((regs), 1); \ |
| 151 | } while (0) |
| 152 | |
| 153 | #define set_code_fail(regs, code) do { \ |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 154 | set_fail(regs); \ |
Kevin O'Connor | f205f9f | 2008-03-09 16:11:49 -0400 | [diff] [blame^] | 155 | (regs)->ah = (code); \ |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame] | 156 | } while (0) |
| 157 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 158 | // kbd.c |
| 159 | void handle_15c2(struct bregs *regs); |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 160 | |
| 161 | // clock.c |
| 162 | void handle_1583(struct bregs *regs); |
| 163 | |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 164 | // apm.c |
Kevin O'Connor | 44d6530 | 2008-03-08 11:34:46 -0500 | [diff] [blame] | 165 | void VISIBLE16 handle_1553(struct bregs *regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 166 | |
Kevin O'Connor | a4d3576 | 2008-03-08 15:43:03 -0500 | [diff] [blame] | 167 | // util.c |
| 168 | void usleep(u32 count); |
| 169 | |
| 170 | // rombios32.c |
| 171 | void rombios32_init(void); |
| 172 | |
Kevin O'Connor | 786502d | 2008-02-27 10:41:41 -0500 | [diff] [blame] | 173 | #endif // util.h |