Contiki 3.x
tss.c
1 /*
2  * Copyright (C) 2015-2016, Intel Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "gdt.h"
32 #include "gdt-layout.h"
33 #include "prot-domains.h"
34 #include "segmentation.h"
35 #include "stacks.h"
36 #include "tss.h"
37 
38 /* System-wide TSS */
39 tss_t ATTR_BSS_KERN sys_tss;
40 
41 static segment_desc_t ATTR_BSS_GDT sys_tss_desc;
42 
43 /*---------------------------------------------------------------------------*/
44 /**
45  * \brief Initialize system-wide TSS.
46  */
47 void
48 tss_init(void)
49 {
50  segment_desc_t seg_desc;
51 
52  /* Initialize TSS */
53  KERN_WRITEW(sys_tss.iomap_base, sizeof(sys_tss));
54  KERN_WRITEL(sys_tss.esp2, ((uint32_t)stacks_int) + STACKS_SIZE_INT);
55  KERN_WRITEL(sys_tss.ss2, GDT_SEL_STK_INT);
56  KERN_WRITEL(sys_tss.esp0, ((uint32_t)stacks_exc) + STACKS_SIZE_EXC);
57  KERN_WRITEL(sys_tss.ss0, GDT_SEL_STK_EXC);
58 
59  segment_desc_init(&seg_desc,
60  KERN_DATA_OFF_TO_PHYS_ADDR(&sys_tss), sizeof(sys_tss),
61  SEG_FLAG(DPL, PRIV_LVL_EXC) |
62  SEG_DESCTYPE_SYS | SEG_TYPE_TSS32_AVAIL);
63  gdt_insert(GDT_IDX_OF_DESC(&sys_tss_desc), seg_desc);
64 
65  __asm__ __volatile__ (
66  "ltr %0"
67  :
68  : "r" ((uint16_t)GDT_SEL_OF_DESC(&sys_tss_desc, 0)));
69 }
70 /*---------------------------------------------------------------------------*/
Segment descriptor.
Definition: segmentation.h:89
uint32_t ss0
Stack segment selector for ring 0 code in this task.
Definition: tss.h:48
uint32_t esp2
Stack pointer for ring 2 code in this task.
Definition: tss.h:51
uint32_t esp0
Stack pointer for ring 0 code in this task.
Definition: tss.h:47
Task State Segment.
Definition: tss.h:45
uint32_t ss2
Stack segment selector for ring 2 code in this task.
Definition: tss.h:52
uint16_t iomap_base
Offset from base of TSS to base of IO permission bitmap, if one is installed.
Definition: tss.h:65