blob: 916f672e86b7bf8a4b88d8ab3db4eb27eff2e24e [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 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.
6
7#include "ioport.h" // outb
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -05008#include "biosvar.h" // struct bregs
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05009
10static inline void irq_disable(void) {
11 asm volatile("cli": : :"memory");
12}
13
14static inline void irq_enable(void) {
15 asm volatile("sti": : :"memory");
16}
17
18static inline unsigned long irq_save(void)
19{
20 unsigned long flags;
21 asm volatile("pushfl ; popl %0" : "=g" (flags));
22 irq_disable();
23 return flags;
24}
25
26static inline void irq_restore(unsigned long flags)
27{
28 asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
29}
30
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050031static inline void nop(void)
32{
33 asm volatile("nop");
34}
35
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050036#define DEBUGF(fmt, args...)
37#define BX_PANIC(fmt, args...)
38#define BX_INFO(fmt, args...)
39
40static inline void
41memset(void *s, int c, size_t n)
42{
43 while (n)
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050044 ((char *)s)[--n] = c;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050045}
46
Kevin O'Connor4b60c002008-02-25 22:29:55 -050047static inline void
48eoi_master_pic()
49{
50 outb(PIC1_IRQ5, PORT_PIC1);
51}
52
53static inline void
54eoi_both_pics()
55{
56 outb(PIC2_IRQ13, PORT_PIC2);
57 eoi_master_pic();
58}
59
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050060static inline
61void call16(struct bregs *callregs)
62{
63 asm volatile(
64 "pushfl\n" // Save flags
65 "pushal\n" // Save registers
66 "calll __call16\n"
67 "popal\n"
68 "popfl\n"
69 : : "a" (callregs), "m" (*callregs));
70}
71
72// XXX - this is ugly.
73#ifdef MODE16
74#define call16_int(nr, callregs) do { \
75 struct bregs *__br = (callregs); \
76 extern void irq_trampoline_ ##nr (); \
77 __br->cs = 0xf000; \
78 __br->ip = (u16)&irq_trampoline_ ##nr; \
79 call16(__br); \
80 } while (0)
81#else
82#include "../out/rom16.offset.auto.h"
83#define call16_int(nr, callregs) do { \
84 struct bregs *__br = (callregs); \
85 __br->cs = 0xf000; \
86 __br->ip = OFFSET_irq_trampoline_ ##nr; \
87 call16(__br); \
88 } while (0)
89#endif
90
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050091// output.c
92void bprintf(u16 action, const char *fmt, ...)
93 __attribute__ ((format (printf, 2, 3)));
94struct bregs;
95void __debug_enter(const char *fname, struct bregs *regs);
96void __debug_exit(const char *fname, struct bregs *regs);
97#define debug_enter(regs) \
98 __debug_enter(__func__, regs)
99#define debug_exit(regs) \
100 __debug_exit(__func__, regs)
101#define printf(fmt, args...) \
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500102 bprintf(1, fmt , ##args )
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500103
104// kbd.c
105void handle_15c2(struct bregs *regs);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500106
107// clock.c
108void handle_1583(struct bregs *regs);
109
110// Frequent bios return helper
111#define RET_EUNSUPPORTED 0x86
112static inline void
113handle_ret(struct bregs *regs, u8 code)
114{
115 regs->ah = code;
116 set_cf(regs, code);
117}