OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [example/] [demo_pmp/] [main.c] - Blame information for rev 73

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 73 zero_gravi
// #################################################################################################
2
// # << NEORV32 - Physical Memory Protection Example Program >>                                    #
3
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6
// # Copyright (c) 2022, Stephan Nolting. All rights reserved.                                     #
7
// #                                                                                               #
8
// # Redistribution and use in source and binary forms, with or without modification, are          #
9
// # permitted provided that the following conditions are met:                                     #
10
// #                                                                                               #
11
// # 1. Redistributions of source code must retain the above copyright notice, this list of        #
12
// #    conditions and the following disclaimer.                                                   #
13
// #                                                                                               #
14
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
15
// #    conditions and the following disclaimer in the documentation and/or other materials        #
16
// #    provided with the distribution.                                                            #
17
// #                                                                                               #
18
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
19
// #    endorse or promote products derived from this software without specific prior written      #
20
// #    permission.                                                                                #
21
// #                                                                                               #
22
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
23
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
24
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
25
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
26
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
27
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
28
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
29
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
30
// # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
31
// # ********************************************************************************************* #
32
// # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
33
// #################################################################################################
34
 
35
 
36
/**********************************************************************//**
37
 * @file demo_pmp/main.c
38
 * @author Stephan Nolting
39
 * @brief Physical memory protection (PMP) example program.
40
 **************************************************************************/
41
#include <neorv32.h>
42
 
43
 
44
/**********************************************************************//**
45
 * @name User configuration
46
 **************************************************************************/
47
/**@{*/
48
/** UART BAUD rate */
49
#define BAUD_RATE 19200
50
/**@}*/
51
 
52
 
53
/**********************************************************************//**
54
 * Example variable that will be protected by the PMP
55
 **************************************************************************/
56
uint32_t protected_var[4] = {
57
  0x11223344,
58
  0x55667788,
59
  0x00CAFE00,
60
  0xDEADC0DE
61
};
62
 
63
 
64
/**********************************************************************//**
65
 * Main function
66
 *
67
 * @note This program requires the CPU PMP extension (with at least 2 regions) and UART0.
68
 *
69
 * @return 0 if execution was successful
70
 **************************************************************************/
71
int main() {
72
 
73
  // initialize NEORV32 run-time environment
74
  neorv32_rte_setup();
75
 
76
  // setup UART0 at default baud rate, no parity bits, no HW flow control
77
  neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
78
 
79
  // check if UART0 is implemented
80
  if (neorv32_uart0_available() == 0) {
81
    return 1; // UART0 not available, exit
82
  }
83
 
84
  // check if PMP is implemented at all
85
  if ((neorv32_cpu_csr_read(CSR_MXISA) & (1 << CSR_MXISA_PMP)) == 0) {
86
    neorv32_uart0_printf("ERROR! PMP CPU extension not implemented!\n");
87
    return 1;
88
  }
89
 
90
 
91
  // intro
92
  neorv32_uart0_printf("\n<<< NEORV32 Physical Memory Protection (PMP) Example Program >>>\n\n");
93
 
94
  neorv32_uart0_printf("NOTE: This program requires at least 2 PMP regions (PMP_NUM_REGIONS >= 2)\n"
95
                       "      and a minimal granularity of 4 bytes (PMP_MIN_GRANULARITY = 4).\n\n");
96
 
97
  neorv32_uart0_printf("NOTE: A 4-word array 'protected_var[4]' is created, which will be probed from\n"
98
                       "      **machine-mode**. It provides the following access rights:\n"
99
                       "      - NO_EXECUTE\n"
100
                       "      - NO_WRITE\n"
101
                       "      - READ\n"
102
                       "      - LOCKED - also enforce access rights for machine-mode software\n\n");
103
 
104
 
105
  // show PMP configuration
106
  neorv32_uart0_printf("PMP hardware configuration:\n");
107
  neorv32_uart0_printf("> Number of regions: %u\n", neorv32_cpu_pmp_get_num_regions());
108
  neorv32_uart0_printf("> Min. granularity:  %u bytes (minimal region size)\n\n", neorv32_cpu_pmp_get_granularity());
109
 
110
 
111
  // The "protected_var" variable will be protected: No execute and no write access, just allow read access
112
 
113
  // create protected region
114
  int pmp_status;
115
  uint8_t permissions;
116
  neorv32_uart0_printf("Creating protected regions (any access within [REGION_BEGIN <= address < REGION_END] will match the PMP rules)...\n");
117
 
118
  // any access in "region_begin <= address < region_end" will match the PMP rule
119
  uint32_t region_begin = (uint32_t)(&protected_var[0]);
120
  uint32_t region_end   = (uint32_t)(&protected_var[4]) + 4;
121
  neorv32_uart0_printf("REGION_BEGIN = 0x%x\n", region_begin);
122
  neorv32_uart0_printf("REGION_END   = 0x%x\n", region_end);
123
 
124
  // base (region begin)
125
  permissions = PMP_OFF << PMPCFG_A_LSB; // mode = OFF
126
  neorv32_uart0_printf("> Region begin (PMP entry 0): Base = 0x%x, Mode = OFF (base of region) ", region_begin);
127
  pmp_status = neorv32_cpu_pmp_configure_region(0, region_begin, permissions);
128
  if (pmp_status) {
129
    neorv32_uart0_printf("[FAILED]\n");
130
  }
131
  else {
132
    neorv32_uart0_printf("[ok]\n");
133
  }
134
 
135
  // bound (region end)
136
  permissions = (PMP_TOR << PMPCFG_A_LSB) | // enable entry as TOR = top of region
137
                (0 << PMPCFG_X) | // no "execute" permission
138
                (0 << PMPCFG_W) | // no "write" permission
139
                (1 << PMPCFG_R) | // set "read" permission
140
                (1 << PMPCFG_L);  // locked: also enforce PMP rule for machine-mode software
141
  neorv32_uart0_printf("> Region end   (PMP entry 1): Base = 0x%x, Mode = TOR (top of region)  ", region_end);
142
  pmp_status = neorv32_cpu_pmp_configure_region(1, region_end, permissions);
143
  if (pmp_status) {
144
    neorv32_uart0_printf("[FAILED]\n");
145
  }
146
  else {
147
    neorv32_uart0_printf("[ok]\n");
148
  }
149
 
150
  // test access
151
  neorv32_uart0_printf("\nTesting access to 'protected_var' - invalid accesses will raise an exception, which will be\n"
152
                       "captured by the NEORV32 runtime environment's dummy/debug handlers ('<RTE> ... </RTE>').\n\n");
153
 
154
  neorv32_uart0_printf("Reading protected_var[0] = 0x%x\n", protected_var[0]);
155
  neorv32_uart0_printf("Reading protected_var[1] = 0x%x\n", protected_var[1]);
156
  neorv32_uart0_printf("Reading protected_var[2] = 0x%x\n", protected_var[2]);
157
  neorv32_uart0_printf("Reading protected_var[3] = 0x%x\n\n", protected_var[3]);
158
 
159
  neorv32_uart0_printf("Trying to write protected_var[0]... ");
160
  protected_var[0] = 0; // should fail!
161
  neorv32_uart0_printf("Trying to write protected_var[1]... ");
162
  protected_var[1] = 0; // should fail!
163
  neorv32_uart0_printf("Trying to write protected_var[2]... ");
164
  protected_var[2] = 0; // should fail!
165
  neorv32_uart0_printf("Trying to write protected_var[3]... ");
166
  protected_var[3] = 0; // should fail!
167
 
168
  neorv32_uart0_printf("\nReading again protected_var[0] = 0x%x\n", protected_var[0]);
169
  neorv32_uart0_printf("Reading again protected_var[1] = 0x%x\n", protected_var[1]);
170
  neorv32_uart0_printf("Reading again protected_var[2] = 0x%x\n", protected_var[2]);
171
  neorv32_uart0_printf("Reading again protected_var[3] = 0x%x\n\n", protected_var[3]);
172
 
173
 
174
  neorv32_uart0_printf("\nPMP demo program completed.\n");
175
 
176
  return 0;
177
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.