1/**
2 * \file
3 * \brief x86-32 interrupt/exception handling
4 */
5
6/*
7 * Copyright (c) 2007, 2008, 2009, 2010, ETH Zurich.
8 * All rights reserved.
9 *
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
13 */
14
15/*-
16 * Copyright (c) 1989, 1990 William F. Jolitz
17 * Copyright (c) 1990 The Regents of the University of California.
18 * All rights reserved.
19 *
20 * This code is derived from software contributed to Berkeley by
21 * William Jolitz.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 *    notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 *    notice, this list of conditions and the following disclaimer in the
30 *    documentation and/or other materials provided with the distribution.
31 * 4. Neither the name of the University nor the names of its contributors
32 *    may be used to endorse or promote products derived from this software
33 *    without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 *
47 *      from: @(#)segments.h    7.1 (Berkeley) 5/9/91
48 * $FreeBSD$
49 */
50
51#ifndef KERNEL_IRQ_H
52#define KERNEL_IRQ_H
53
54/*
55 * AMD64 Segmentation Data Structures and definitions
56 */
57
58/*
59 * Selectors
60 */
61
62#define SEL_RPL_MASK    3       /* requester priv level */
63#define ISPL(s) ((s)&3)         /* what is the priority level of a selector */
64#define SEL_KPL 0               /* kernel priority level */
65#define SEL_UPL 3               /* user priority level */
66#define ISLDT(s)        ((s)&SEL_LDT)   /* is it local or global */
67#define SEL_LDT 4               /* local descriptor table */
68#define IDXSEL(s)       (((s)>>3) & 0x1fff)             /* index of selector */
69#define LSEL(s,r)       (((s)<<3) | SEL_LDT | r)        /* a local selector */
70#define GSEL(s,r)       (((s)<<3) | r)                  /* a global selector */
71
72/**
73 * Gate descriptors (e.g. indirect descriptors, trap, interrupt etc. 128 bit)
74 * Only interrupt and trap gates have gd_ist.
75 */
76struct  gate_descriptor {
77    uint64_t gd_looffset:16;       /* gate offset (lsb) */
78    uint64_t gd_selector:16;       /* gate segment selector */
79    uint64_t gd_xx:5;              /* reserved */
80    uint64_t gd_mbz:3;             /* must be zero */
81    uint64_t gd_type:5;            /* segment type */
82    uint64_t gd_dpl:2;             /* segment descriptor priority level */
83    uint64_t gd_p:1;               /* segment descriptor present */
84    uint64_t gd_hioffset:16;       /* gate offset (msb) */
85} __attribute__((packed));
86
87        /* system segments and gate types */
88#define SDT_SYSNULL      0      /* system null */
89#define SDT_SYSLDT       2      /* system local descriptor table */
90#define SDT_SYSTSS       9      /* system available 32 bit TSS */
91#define SDT_SYSBSY      11      /* system busy 32 bit TSS */
92#define SDT_SYSCGT      12      /* system 32 bit call gate */
93#define SDT_SYSIGT      14      /* system 32 bit interrupt gate */
94#define SDT_SYSTGT      15      /* system 32 bit trap gate */
95
96        /* memory segment types */
97#define SDT_MEMRO       16      /* memory read only */
98#define SDT_MEMROA      17      /* memory read only accessed */
99#define SDT_MEMRW       18      /* memory read write */
100#define SDT_MEMRWA      19      /* memory read write accessed */
101#define SDT_MEMROD      20      /* memory read only expand dwn limit */
102#define SDT_MEMRODA     21      /* memory read only expand dwn limit accessed */
103#define SDT_MEMRWD      22      /* memory read write expand dwn limit */
104#define SDT_MEMRWDA     23      /* memory read write expand dwn limit accessed */
105#define SDT_MEME        24      /* memory execute only */
106#define SDT_MEMEA       25      /* memory execute only accessed */
107#define SDT_MEMER       26      /* memory execute read */
108#define SDT_MEMERA      27      /* memory execute read accessed */
109#define SDT_MEMEC       28      /* memory execute only conforming */
110#define SDT_MEMEAC      29      /* memory execute only accessed conforming */
111#define SDT_MEMERC      30      /* memory execute read conforming */
112#define SDT_MEMERAC     31      /* memory execute read accessed conforming */
113
114/*
115 * Size of IDT table
116 */
117#define NIDT    256             /* 32 reserved, 16 h/w, 0 s/w, linux's 0x80 */
118
119/// Number of (reserved) hardware exceptions
120#define NEXCEPTIONS             32
121
122/// Size of hardware IRQ dispatch table == #NIDT - #NEXCEPTIONS exceptions
123#define NDISPATCH               (NIDT - NEXCEPTIONS)
124
125
126/*
127 * Entries in the Global Descriptor Table (GDT)
128 */
129#define NULL_SEL        0       /**< Null descriptor */
130#define KCODE_SEL       1       /**< Kernel code descriptor */
131#define KSTACK_SEL      2       /**< Shared user/kernel stack descriptor */
132#define USTACK_SEL      3       /**< User stack descriptor */
133#define UCODE_SEL       4       /**< User code descriptor */
134#define TSS_SEL         5       /**< Task State Segment (TSS) */
135#define DISP_SEL        6       /**< Dispatcher pointer */
136#define NGDT_MEM        7       /**< Number of descriptors */
137
138/**
139 * region descriptors, used to load gdt/idt tables before segments yet exist.
140 */
141struct region_descriptor {
142    uint16_t rd_limit;          /**< segment extent */
143    uint64_t rd_base;           /**< base address  */
144} __attribute__((packed));
145
146/**
147 * \brief Segment descriptor
148 */
149union segment_descriptor {
150    uint64_t raw;
151    struct {
152        uint64_t lo_limit:16;
153        uint64_t lo_base:24;
154        uint64_t type:4;
155        uint64_t system_desc:1;
156        uint64_t privilege_level:2;
157        uint64_t present:1;
158        uint64_t hi_limit:4;
159        uint64_t available:1;
160        uint64_t long_mode:1;
161        uint64_t operation_size:1;
162        uint64_t granularity:1;
163        uint64_t hi_base:8;
164    } d;
165    struct {
166        uint64_t lo_limit:16;
167        uint64_t lo_base:24;
168        uint64_t type:4;
169        uint64_t always0:1;
170        uint64_t privilege_level:2;
171        uint64_t present:1;
172        uint64_t hi_limit:4;
173        uint64_t available:1;
174        uint64_t always0_1:2;
175        uint64_t granularity:1;
176        uint64_t hi_base:8;
177    } tss;
178};
179
180struct task_state_segment {
181    uint16_t    previous_task_link;
182    uint16_t    reserved;
183    uint32_t    esp0;
184    uint32_t    ss0;
185    uint32_t    esp1;
186    uint32_t    ss1;
187    uint32_t    esp2;
188    uint32_t    ss2;
189    uint32_t    cr3_pdbr;
190    uint32_t    eip;
191    uint32_t    eflags;
192    uint32_t    eax;
193    uint32_t    ecx;
194    uint32_t    edx;
195    uint32_t    ebx;
196    uint32_t    esp;
197    uint32_t    ebp;
198    uint32_t    esi;
199    uint32_t    edi;
200    uint32_t    es;
201    uint32_t    cs;
202    uint32_t    ss;
203    uint32_t    ds;
204    uint32_t    fs;
205    uint32_t    gs;
206    uint32_t    ldt_segment_selector;
207    uint16_t    T;
208    uint16_t    iomap_base;
209} __attribute__ ((packed));
210
211void setup_default_idt(void);
212
213errval_t irq_connect(struct capability *dest_cap, capaddr_t endpoint_adr);
214errval_t irq_table_alloc(int *outvec);
215errval_t irq_table_set(unsigned int nidt, capaddr_t endpoint);
216errval_t irq_table_delete(unsigned int nidt);
217struct kcb;
218errval_t irq_table_notify_domains(struct kcb *kcb);
219errval_t irq_table_alloc_dest_cap(uint8_t dcn_vbits, capaddr_t dcn, capaddr_t out_cap_addr);
220
221
222#endif
223