2 ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
8 #include <boot/stage2.h>
9 #include <newos/types.h>
11 extern void _start(unsigned int mem
, void *extended_mem_block
, unsigned int extended_mem_count
, int in_vesa
, unsigned int vesa_ptr
, unsigned int console_ptr
);
12 extern void clearscreen(void);
13 extern void puts(const char *str
);
14 extern int dprintf(const char *fmt
, ...);
15 extern void sleep(uint64 time
);
16 extern uint64
system_time(void);
17 extern void execute_n_instructions(int count
);
18 void system_time_setup(long a
);
20 unsigned long get_rflags(void);
21 void set_rflags(unsigned long val
);
22 void cpuid(unsigned int selector
, unsigned int *data
);
24 //void put_uint_dec(unsigned int a);
25 //void put_uint_hex(unsigned int a);
26 //void nmessage(const char *str1, unsigned int a, const char *str2);
27 //void nmessage2(const char *str1, unsigned int a, const char *str2, unsigned int b, const char *str3);
29 #define ROUNDUP(a, b) (((a) + ((b) - 1)) & (~((b) - 1)))
30 #define ROUNDOWN(a, b) (((a) / (b)) * (b))
32 #define PAGE_SIZE 0x1000
33 #define KERNEL_BASE 0x80000000
34 #define KERNEL_ENTRY 0x80000080
36 #define DEFAULT_PAGE_FLAGS (1 | 2) // present/rw
37 #define SCREEN_WIDTH 80
38 #define SCREEN_HEIGHT 25
39 #define ADDR_MASK 0xfffff000
43 extern int smp_boot(kernel_args
*ka
, unsigned int kernel_entry
);
44 extern void smp_trampoline(void);
45 extern void smp_trampoline_end(void);
47 #define MP_FLT_SIGNATURE '_PM_'
48 #define MP_CTH_SIGNATURE 'PCMP'
50 #define APIC_DM_INIT (5 << 8)
51 #define APIC_DM_STARTUP (6 << 8)
52 #define APIC_LEVEL_TRIG (1 << 14)
53 #define APIC_ASSERT (1 << 13)
55 #define APIC_ENABLE 0x100
56 #define APIC_FOCUS (~(1 << 9))
57 #define APIC_SIV (0xff)
59 #define APIC_ID ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x020))
60 #define APIC_VERSION ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x030))
61 #define APIC_TPRI ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x080))
62 #define APIC_EOI ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x0b0))
63 #define APIC_LDR ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x0d0))
64 #define APIC_SIVR ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x0f0))
65 #define APIC_ESR ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x280))
66 #define APIC_ICR1 ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x300))
67 #define APIC_ICR2 ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x310))
68 #define APIC_LVTT ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x320))
69 #define APIC_LINT0 ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x350))
70 #define APIC_LINT1 ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x360))
71 #define APIC_LVT3 ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x370))
72 #define APIC_ICRT ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x380))
73 #define APIC_CCRT ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x390))
74 #define APIC_TDCR ((unsigned int *) ((unsigned int) ka->arch_args.apic + 0x3e0))
76 #define APIC_TDCR_2 0x00
77 #define APIC_TDCR_4 0x01
78 #define APIC_TDCR_8 0x02
79 #define APIC_TDCR_16 0x03
80 #define APIC_TDCR_32 0x08
81 #define APIC_TDCR_64 0x09
82 #define APIC_TDCR_128 0x0a
83 #define APIC_TDCR_1 0x0b
85 #define APIC_LVTT_MASK 0x000310ff
86 #define APIC_LVTT_VECTOR 0x000000ff
87 #define APIC_LVTT_DS 0x00001000
88 #define APIC_LVTT_M 0x00010000
89 #define APIC_LVTT_TM 0x00020000
91 #define APIC_LVT_DM_ExtINT 0x00000700
92 #define APIC_LVT_DM_NMI 0x00000400
93 #define APIC_LVT_IIPP 0x00002000
94 #define APIC_LVT_TM 0x00008000
95 #define APIC_LVT_M 0x00010000
96 #define APIC_LVT_OS 0x00020000
97 #define APIC_TPR_PRIO 0x000000ff
98 #define APIC_TPR_INT 0x000000f0
99 #define APIC_TPR_SUB 0x0000000f
101 #define APIC_SVR_SWEN 0x00000100
102 #define APIC_SVR_FOCUS 0x00000200
104 #define APIC_DEST_STARTUP 0x00600
106 #define LOPRIO_LEVEL 0x00000010
108 #define APIC_DEST_FIELD (0)
109 #define APIC_DEST_SELF (1 << 18)
110 #define APIC_DEST_ALL (2 << 18)
111 #define APIC_DEST_ALL_BUT_SELF (3 << 18)
113 #define IOAPIC_ID 0x0
114 #define IOAPIC_VERSION 0x1
115 #define IOAPIC_ARB 0x2
116 #define IOAPIC_REDIR_TABLE 0x10
118 #define IPI_CACHE_FLUSH 0x40
119 #define IPI_INV_TLB 0x41
120 #define IPI_INV_PTE 0x42
121 #define IPI_INV_RESCHED 0x43
122 #define IPI_STOP 0x44
126 #define MP_EXT_IO_APIC 2
127 #define MP_EXT_IO_INT 3
128 #define MP_EXT_LOCAL_INT 4
130 #define MP_EXT_PE_LEN 20
131 #define MP_EXT_BUS_LEN 8
132 #define MP_EXT_IO_APIC_LEN 8
133 #define MP_EXT_IO_INT_LEN 8
134 #define MP_EXT_LOCAL_INT_LEN 8
136 struct mp_config_table
{
137 unsigned int signature
; /* "PCMP" */
138 unsigned short table_len
; /* length of this structure */
139 unsigned char mp_rev
; /* spec supported, 1 for 1.1 or 4 for 1.4 */
140 unsigned char checksum
; /* checksum, all bytes add up to zero */
141 char oem
[8]; /* oem identification, not null-terminated */
142 char product
[12]; /* product name, not null-terminated */
143 void *oem_table_ptr
; /* addr of oem-defined table, zero if none */
144 unsigned short oem_len
; /* length of oem table */
145 unsigned short num_entries
; /* number of entries in base table */
146 unsigned int apic
; /* address of apic */
147 unsigned short ext_len
; /* length of extended section */
148 unsigned char ext_checksum
; /* checksum of extended table entries */
151 struct mp_flt_struct
{
152 unsigned int signature
; /* "_MP_" */
153 struct mp_config_table
*mpc
; /* address of mp configuration table */
154 unsigned char mpc_len
; /* length of this structure in 16-byte units */
155 unsigned char mp_rev
; /* spec supported, 1 for 1.1 or 4 for 1.4 */
156 unsigned char checksum
; /* checksum, all bytes add up to zero */
157 unsigned char mp_feature_1
; /* mp system configuration type if no mpc */
158 unsigned char mp_feature_2
; /* imcrp */
159 unsigned char mp_feature_3
, mp_feature_4
, mp_feature_5
; /* reserved */
165 unsigned char apic_id
;
166 unsigned char apic_version
;
167 unsigned char cpu_flags
;
168 unsigned int signature
; /* stepping, model, family, each four bits */
169 unsigned int feature_flags
;
170 unsigned int res1
, res2
;
176 unsigned char ioapic_id
;
177 unsigned char ioapic_version
;
178 unsigned char ioapic_flags
;
185 unsigned char bus_id
;