Contiki 3.x
paging-prot-domains.h
1 /*
2  * Copyright (C) 2015, 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 #ifndef CPU_X86_MM_PAGING_PROT_DOMAINS_H_
32 #define CPU_X86_MM_PAGING_PROT_DOMAINS_H_
33 
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <stdbool.h>
37 #include "dma.h"
38 #include "helpers.h"
39 #include "paging.h"
40 #include "syscalls-int.h"
41 
42 struct dom_kern_data {
43  /** Base physical address of optional MMIO region */
44  uintptr_t mmio;
45  /** Number of (contiguous) pages in MMIO region */
46  size_t mmio_sz;
47  /** Base physical address of optional metadata region */
48  uintptr_t meta;
49  /** Number of (contiguous) pages in metadata region */
50  size_t meta_sz;
51  /** Flags are defined with the prefix PROT_DOMAINS_FLAG in prot-domains.h */
52  uint32_t flags;
53  /**
54  * Original return address from call stack when this protection domain
55  * invoked some other protection domain. This serves to control the return
56  * entrypoint. The callee is not permitted to modify this value (unless the
57  * callee is the kernel protection domain).
58  */
59  uintptr_t orig_ret_addr;
60 
61  /* align to next-larger power of 2 to enable usage of shifting instead of
62  * multiplication to index an array of these structures.
63  */
64 } __attribute__((aligned(32)));
65 
66 /** Linear base address at which to map the MMIO region. */
67 #define PROT_DOMAINS_MMIO_LINEAR_BASE (MIN_PAGE_SIZE + (uintptr_t)&_ebss_kern_addr)
68 
69 /** Maximum supported size of MMIO region */
70 #define PROT_DOMAINS_MAX_MMIO_SZ 0x4000
71 
72 /** Linear base address at which to map the metadata region */
73 #define PROT_DOMAINS_META_LINEAR_BASE \
74  (MIN_PAGE_SIZE + (PROT_DOMAINS_MMIO_LINEAR_BASE + PROT_DOMAINS_MAX_MMIO_SZ))
75 
76 #define PROT_DOMAINS_META_OFF_TO_PHYS(off, meta_phys_base) \
77  ((meta_phys_base) + ((off) - PROT_DOMAINS_META_LINEAR_BASE))
78 
79 /** Any MMIO region mapping always starts at a particular linear address. */
80 #define PROT_DOMAINS_MMIO(dcd) PROT_DOMAINS_MMIO_LINEAR_BASE
81 /**
82  * Any metadata region mapping always starts at a particular linear address.
83  */
84 #define PROT_DOMAINS_META(dcd) PROT_DOMAINS_META_LINEAR_BASE
85 
86 #define PROT_DOMAINS_ENTER_ISR(exc) \
87  PROT_DOMAINS_ENTER_ISR_COMMON(exc)
88 #define PROT_DOMAINS_LEAVE_ISR(exc) PROT_DOMAINS_LEAVE_ISR_COMMON(exc)
89 
90 /* Enable paging */
91 #define CR0_PG BIT(31)
92 /* Enable write protection in supervisor mode */
93 #define CR0_WP BIT(16)
94 /* Enable protected mode */
95 #define CR0_PE BIT(0)
96 
97 /**
98  * \brief Enable or disable write protection enforcement in supervisor mode.
99  * When disabled, supervisory code (i.e. code running at ring levels
100  * 0-2) is permitted to write to pages that are marked read-only in
101  * page tables.
102  *
103  * \param en Set to true to enable write protection enforcement.
104  */
105 static inline void prot_domains_set_wp(bool en)
106 {
107  uint32_t cr0_val = CR0_PG | CR0_PE;
108  if(en) {
109  cr0_val |= CR0_WP;
110  }
111  __asm__ __volatile__ ("mov %0, %%cr0" :: "r"(cr0_val));
112 }
113 
114 #endif /* CPU_X86_MM_PAGING_PROT_DOMAINS_H_ */
#define __attribute__(nothing)
Define attribute to nothing since it isn't handled by IAR.
Definition: iar.h:194