xref: /openbmc/qemu/target/riscv/pmp.c (revision b84ffd6e)
1 /*
2  * QEMU RISC-V PMP (Physical Memory Protection)
3  *
4  * Author: Daire McNamara, daire.mcnamara@emdalo.com
5  *         Ivan Griffin, ivan.griffin@emdalo.com
6  *
7  * This provides a RISC-V Physical Memory Protection implementation
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms and conditions of the GNU General Public License,
11  * version 2 or later, as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "qemu/osdep.h"
23 #include "qemu/log.h"
24 #include "qapi/error.h"
25 #include "cpu.h"
26 #include "trace.h"
27 #include "exec/exec-all.h"
28 
29 static void pmp_write_cfg(CPURISCVState *env, uint32_t addr_index,
30                           uint8_t val);
31 static uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t addr_index);
32 static void pmp_update_rule(CPURISCVState *env, uint32_t pmp_index);
33 
34 /*
35  * Accessor method to extract address matching type 'a field' from cfg reg
36  */
37 static inline uint8_t pmp_get_a_field(uint8_t cfg)
38 {
39     uint8_t a = cfg >> 3;
40     return a & 0x3;
41 }
42 
43 /*
44  * Check whether a PMP is locked or not.
45  */
46 static inline int pmp_is_locked(CPURISCVState *env, uint32_t pmp_index)
47 {
48 
49     if (env->pmp_state.pmp[pmp_index].cfg_reg & PMP_LOCK) {
50         return 1;
51     }
52 
53     /* Top PMP has no 'next' to check */
54     if ((pmp_index + 1u) >= MAX_RISCV_PMPS) {
55         return 0;
56     }
57 
58     return 0;
59 }
60 
61 /*
62  * Count the number of active rules.
63  */
64 uint32_t pmp_get_num_rules(CPURISCVState *env)
65 {
66      return env->pmp_state.num_rules;
67 }
68 
69 /*
70  * Accessor to get the cfg reg for a specific PMP/HART
71  */
72 static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)
73 {
74     if (pmp_index < MAX_RISCV_PMPS) {
75         return env->pmp_state.pmp[pmp_index].cfg_reg;
76     }
77 
78     return 0;
79 }
80 
81 
82 /*
83  * Accessor to set the cfg reg for a specific PMP/HART
84  * Bounds checks and relevant lock bit.
85  */
86 static void pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
87 {
88     if (pmp_index < MAX_RISCV_PMPS) {
89         bool locked = true;
90 
91         if (riscv_cpu_cfg(env)->epmp) {
92             /* mseccfg.RLB is set */
93             if (MSECCFG_RLB_ISSET(env)) {
94                 locked = false;
95             }
96 
97             /* mseccfg.MML is not set */
98             if (!MSECCFG_MML_ISSET(env) && !pmp_is_locked(env, pmp_index)) {
99                 locked = false;
100             }
101 
102             /* mseccfg.MML is set */
103             if (MSECCFG_MML_ISSET(env)) {
104                 /* not adding execute bit */
105                 if ((val & PMP_LOCK) != 0 && (val & PMP_EXEC) != PMP_EXEC) {
106                     locked = false;
107                 }
108                 /* shared region and not adding X bit */
109                 if ((val & PMP_LOCK) != PMP_LOCK &&
110                     (val & 0x7) != (PMP_WRITE | PMP_EXEC)) {
111                     locked = false;
112                 }
113             }
114         } else {
115             if (!pmp_is_locked(env, pmp_index)) {
116                 locked = false;
117             }
118         }
119 
120         if (locked) {
121             qemu_log_mask(LOG_GUEST_ERROR, "ignoring pmpcfg write - locked\n");
122         } else {
123             env->pmp_state.pmp[pmp_index].cfg_reg = val;
124             pmp_update_rule(env, pmp_index);
125         }
126     } else {
127         qemu_log_mask(LOG_GUEST_ERROR,
128                       "ignoring pmpcfg write - out of bounds\n");
129     }
130 }
131 
132 static void pmp_decode_napot(target_ulong a, target_ulong *sa,
133                              target_ulong *ea)
134 {
135     /*
136      * aaaa...aaa0   8-byte NAPOT range
137      * aaaa...aa01   16-byte NAPOT range
138      * aaaa...a011   32-byte NAPOT range
139      * ...
140      * aa01...1111   2^XLEN-byte NAPOT range
141      * a011...1111   2^(XLEN+1)-byte NAPOT range
142      * 0111...1111   2^(XLEN+2)-byte NAPOT range
143      * 1111...1111   Reserved
144      */
145     a = (a << 2) | 0x3;
146     *sa = a & (a + 1);
147     *ea = a | (a + 1);
148 }
149 
150 void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)
151 {
152     uint8_t this_cfg = env->pmp_state.pmp[pmp_index].cfg_reg;
153     target_ulong this_addr = env->pmp_state.pmp[pmp_index].addr_reg;
154     target_ulong prev_addr = 0u;
155     target_ulong sa = 0u;
156     target_ulong ea = 0u;
157 
158     if (pmp_index >= 1u) {
159         prev_addr = env->pmp_state.pmp[pmp_index - 1].addr_reg;
160     }
161 
162     switch (pmp_get_a_field(this_cfg)) {
163     case PMP_AMATCH_OFF:
164         sa = 0u;
165         ea = -1;
166         break;
167 
168     case PMP_AMATCH_TOR:
169         sa = prev_addr << 2; /* shift up from [xx:0] to [xx+2:2] */
170         ea = (this_addr << 2) - 1u;
171         if (sa > ea) {
172             sa = ea = 0u;
173         }
174         break;
175 
176     case PMP_AMATCH_NA4:
177         sa = this_addr << 2; /* shift up from [xx:0] to [xx+2:2] */
178         ea = (sa + 4u) - 1u;
179         break;
180 
181     case PMP_AMATCH_NAPOT:
182         pmp_decode_napot(this_addr, &sa, &ea);
183         break;
184 
185     default:
186         sa = 0u;
187         ea = 0u;
188         break;
189     }
190 
191     env->pmp_state.addr[pmp_index].sa = sa;
192     env->pmp_state.addr[pmp_index].ea = ea;
193 }
194 
195 void pmp_update_rule_nums(CPURISCVState *env)
196 {
197     int i;
198 
199     env->pmp_state.num_rules = 0;
200     for (i = 0; i < MAX_RISCV_PMPS; i++) {
201         const uint8_t a_field =
202             pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg);
203         if (PMP_AMATCH_OFF != a_field) {
204             env->pmp_state.num_rules++;
205         }
206     }
207 }
208 
209 /*
210  * Convert cfg/addr reg values here into simple 'sa' --> start address and 'ea'
211  *   end address values.
212  *   This function is called relatively infrequently whereas the check that
213  *   an address is within a pmp rule is called often, so optimise that one
214  */
215 static void pmp_update_rule(CPURISCVState *env, uint32_t pmp_index)
216 {
217     pmp_update_rule_addr(env, pmp_index);
218     pmp_update_rule_nums(env);
219 }
220 
221 static int pmp_is_in_range(CPURISCVState *env, int pmp_index,
222                            target_ulong addr)
223 {
224     int result = 0;
225 
226     if ((addr >= env->pmp_state.addr[pmp_index].sa) &&
227         (addr <= env->pmp_state.addr[pmp_index].ea)) {
228         result = 1;
229     } else {
230         result = 0;
231     }
232 
233     return result;
234 }
235 
236 /*
237  * Check if the address has required RWX privs when no PMP entry is matched.
238  */
239 static bool pmp_hart_has_privs_default(CPURISCVState *env, target_ulong addr,
240                                        target_ulong size, pmp_priv_t privs,
241                                        pmp_priv_t *allowed_privs,
242                                        target_ulong mode)
243 {
244     bool ret;
245 
246     if (MSECCFG_MMWP_ISSET(env)) {
247         /*
248          * The Machine Mode Whitelist Policy (mseccfg.MMWP) is set
249          * so we default to deny all, even for M-mode.
250          */
251         *allowed_privs = 0;
252         return false;
253     } else if (MSECCFG_MML_ISSET(env)) {
254         /*
255          * The Machine Mode Lockdown (mseccfg.MML) bit is set
256          * so we can only execute code in M-mode with an applicable
257          * rule. Other modes are disabled.
258          */
259         if (mode == PRV_M && !(privs & PMP_EXEC)) {
260             ret = true;
261             *allowed_privs = PMP_READ | PMP_WRITE;
262         } else {
263             ret = false;
264             *allowed_privs = 0;
265         }
266 
267         return ret;
268     }
269 
270     if (!riscv_cpu_cfg(env)->pmp || (mode == PRV_M)) {
271         /*
272          * Privileged spec v1.10 states if HW doesn't implement any PMP entry
273          * or no PMP entry matches an M-Mode access, the access succeeds.
274          */
275         ret = true;
276         *allowed_privs = PMP_READ | PMP_WRITE | PMP_EXEC;
277     } else {
278         /*
279          * Other modes are not allowed to succeed if they don't * match a rule,
280          * but there are rules. We've checked for no rule earlier in this
281          * function.
282          */
283         ret = false;
284         *allowed_privs = 0;
285     }
286 
287     return ret;
288 }
289 
290 
291 /*
292  * Public Interface
293  */
294 
295 /*
296  * Check if the address has required RWX privs to complete desired operation
297  * Return true if a pmp rule match or default match
298  * Return false if no match
299  */
300 bool pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
301                         target_ulong size, pmp_priv_t privs,
302                         pmp_priv_t *allowed_privs, target_ulong mode)
303 {
304     int i = 0;
305     bool ret = false;
306     int pmp_size = 0;
307     target_ulong s = 0;
308     target_ulong e = 0;
309 
310     /* Short cut if no rules */
311     if (0 == pmp_get_num_rules(env)) {
312         return pmp_hart_has_privs_default(env, addr, size, privs,
313                                           allowed_privs, mode);
314     }
315 
316     if (size == 0) {
317         if (riscv_cpu_cfg(env)->mmu) {
318             /*
319              * If size is unknown (0), assume that all bytes
320              * from addr to the end of the page will be accessed.
321              */
322             pmp_size = -(addr | TARGET_PAGE_MASK);
323         } else {
324             pmp_size = sizeof(target_ulong);
325         }
326     } else {
327         pmp_size = size;
328     }
329 
330     /*
331      * 1.10 draft priv spec states there is an implicit order
332      * from low to high
333      */
334     for (i = 0; i < MAX_RISCV_PMPS; i++) {
335         s = pmp_is_in_range(env, i, addr);
336         e = pmp_is_in_range(env, i, addr + pmp_size - 1);
337 
338         /* partially inside */
339         if ((s + e) == 1) {
340             qemu_log_mask(LOG_GUEST_ERROR,
341                           "pmp violation - access is partially inside\n");
342             ret = false;
343             break;
344         }
345 
346         /* fully inside */
347         const uint8_t a_field =
348             pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg);
349 
350         /*
351          * Convert the PMP permissions to match the truth table in the
352          * ePMP spec.
353          */
354         const uint8_t epmp_operation =
355             ((env->pmp_state.pmp[i].cfg_reg & PMP_LOCK) >> 4) |
356             ((env->pmp_state.pmp[i].cfg_reg & PMP_READ) << 2) |
357             (env->pmp_state.pmp[i].cfg_reg & PMP_WRITE) |
358             ((env->pmp_state.pmp[i].cfg_reg & PMP_EXEC) >> 2);
359 
360         if (((s + e) == 2) && (PMP_AMATCH_OFF != a_field)) {
361             /*
362              * If the PMP entry is not off and the address is in range,
363              * do the priv check
364              */
365             if (!MSECCFG_MML_ISSET(env)) {
366                 /*
367                  * If mseccfg.MML Bit is not set, do pmp priv check
368                  * This will always apply to regular PMP.
369                  */
370                 *allowed_privs = PMP_READ | PMP_WRITE | PMP_EXEC;
371                 if ((mode != PRV_M) || pmp_is_locked(env, i)) {
372                     *allowed_privs &= env->pmp_state.pmp[i].cfg_reg;
373                 }
374             } else {
375                 /*
376                  * If mseccfg.MML Bit set, do the enhanced pmp priv check
377                  */
378                 if (mode == PRV_M) {
379                     switch (epmp_operation) {
380                     case 0:
381                     case 1:
382                     case 4:
383                     case 5:
384                     case 6:
385                     case 7:
386                     case 8:
387                         *allowed_privs = 0;
388                         break;
389                     case 2:
390                     case 3:
391                     case 14:
392                         *allowed_privs = PMP_READ | PMP_WRITE;
393                         break;
394                     case 9:
395                     case 10:
396                         *allowed_privs = PMP_EXEC;
397                         break;
398                     case 11:
399                     case 13:
400                         *allowed_privs = PMP_READ | PMP_EXEC;
401                         break;
402                     case 12:
403                     case 15:
404                         *allowed_privs = PMP_READ;
405                         break;
406                     default:
407                         g_assert_not_reached();
408                     }
409                 } else {
410                     switch (epmp_operation) {
411                     case 0:
412                     case 8:
413                     case 9:
414                     case 12:
415                     case 13:
416                     case 14:
417                         *allowed_privs = 0;
418                         break;
419                     case 1:
420                     case 10:
421                     case 11:
422                         *allowed_privs = PMP_EXEC;
423                         break;
424                     case 2:
425                     case 4:
426                     case 15:
427                         *allowed_privs = PMP_READ;
428                         break;
429                     case 3:
430                     case 6:
431                         *allowed_privs = PMP_READ | PMP_WRITE;
432                         break;
433                     case 5:
434                         *allowed_privs = PMP_READ | PMP_EXEC;
435                         break;
436                     case 7:
437                         *allowed_privs = PMP_READ | PMP_WRITE | PMP_EXEC;
438                         break;
439                     default:
440                         g_assert_not_reached();
441                     }
442                 }
443             }
444 
445             /*
446              * If matching address range was found, the protection bits
447              * defined with PMP must be used. We shouldn't fallback on
448              * finding default privileges.
449              */
450             ret = true;
451             break;
452         }
453     }
454 
455     /* No rule matched */
456     if (!ret) {
457         ret = pmp_hart_has_privs_default(env, addr, size, privs,
458                                          allowed_privs, mode);
459     }
460 
461     return ret;
462 }
463 
464 /*
465  * Handle a write to a pmpcfg CSR
466  */
467 void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,
468                       target_ulong val)
469 {
470     int i;
471     uint8_t cfg_val;
472     int pmpcfg_nums = 2 << riscv_cpu_mxl(env);
473 
474     trace_pmpcfg_csr_write(env->mhartid, reg_index, val);
475 
476     for (i = 0; i < pmpcfg_nums; i++) {
477         cfg_val = (val >> 8 * i)  & 0xff;
478         pmp_write_cfg(env, (reg_index * 4) + i, cfg_val);
479     }
480 
481     /* If PMP permission of any addr has been changed, flush TLB pages. */
482     tlb_flush(env_cpu(env));
483 }
484 
485 
486 /*
487  * Handle a read from a pmpcfg CSR
488  */
489 target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index)
490 {
491     int i;
492     target_ulong cfg_val = 0;
493     target_ulong val = 0;
494     int pmpcfg_nums = 2 << riscv_cpu_mxl(env);
495 
496     for (i = 0; i < pmpcfg_nums; i++) {
497         val = pmp_read_cfg(env, (reg_index * 4) + i);
498         cfg_val |= (val << (i * 8));
499     }
500     trace_pmpcfg_csr_read(env->mhartid, reg_index, cfg_val);
501 
502     return cfg_val;
503 }
504 
505 
506 /*
507  * Handle a write to a pmpaddr CSR
508  */
509 void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
510                        target_ulong val)
511 {
512     trace_pmpaddr_csr_write(env->mhartid, addr_index, val);
513 
514     if (addr_index < MAX_RISCV_PMPS) {
515         /*
516          * In TOR mode, need to check the lock bit of the next pmp
517          * (if there is a next).
518          */
519         if (addr_index + 1 < MAX_RISCV_PMPS) {
520             uint8_t pmp_cfg = env->pmp_state.pmp[addr_index + 1].cfg_reg;
521 
522             if (pmp_cfg & PMP_LOCK &&
523                 PMP_AMATCH_TOR == pmp_get_a_field(pmp_cfg)) {
524                 qemu_log_mask(LOG_GUEST_ERROR,
525                               "ignoring pmpaddr write - pmpcfg + 1 locked\n");
526                 return;
527             }
528         }
529 
530         if (!pmp_is_locked(env, addr_index)) {
531             env->pmp_state.pmp[addr_index].addr_reg = val;
532             pmp_update_rule(env, addr_index);
533         } else {
534             qemu_log_mask(LOG_GUEST_ERROR,
535                           "ignoring pmpaddr write - locked\n");
536         }
537     } else {
538         qemu_log_mask(LOG_GUEST_ERROR,
539                       "ignoring pmpaddr write - out of bounds\n");
540     }
541 }
542 
543 
544 /*
545  * Handle a read from a pmpaddr CSR
546  */
547 target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index)
548 {
549     target_ulong val = 0;
550 
551     if (addr_index < MAX_RISCV_PMPS) {
552         val = env->pmp_state.pmp[addr_index].addr_reg;
553         trace_pmpaddr_csr_read(env->mhartid, addr_index, val);
554     } else {
555         qemu_log_mask(LOG_GUEST_ERROR,
556                       "ignoring pmpaddr read - out of bounds\n");
557     }
558 
559     return val;
560 }
561 
562 /*
563  * Handle a write to a mseccfg CSR
564  */
565 void mseccfg_csr_write(CPURISCVState *env, target_ulong val)
566 {
567     int i;
568 
569     trace_mseccfg_csr_write(env->mhartid, val);
570 
571     /* RLB cannot be enabled if it's already 0 and if any regions are locked */
572     if (!MSECCFG_RLB_ISSET(env)) {
573         for (i = 0; i < MAX_RISCV_PMPS; i++) {
574             if (pmp_is_locked(env, i)) {
575                 val &= ~MSECCFG_RLB;
576                 break;
577             }
578         }
579     }
580 
581     if (riscv_cpu_cfg(env)->epmp) {
582         /* Sticky bits */
583         val |= (env->mseccfg & (MSECCFG_MMWP | MSECCFG_MML));
584     } else {
585         val &= ~(MSECCFG_MMWP | MSECCFG_MML | MSECCFG_RLB);
586     }
587 
588     env->mseccfg = val;
589 }
590 
591 /*
592  * Handle a read from a mseccfg CSR
593  */
594 target_ulong mseccfg_csr_read(CPURISCVState *env)
595 {
596     trace_mseccfg_csr_read(env->mhartid, env->mseccfg);
597     return env->mseccfg;
598 }
599 
600 /*
601  * Calculate the TLB size.
602  * It's possible that PMP regions only cover partial of the TLB page, and
603  * this may split the page into regions with different permissions.
604  * For example if PMP0 is (0x80000008~0x8000000F, R) and PMP1 is (0x80000000
605  * ~0x80000FFF, RWX), then region 0x80000008~0x8000000F has R permission, and
606  * the other regions in this page have RWX permissions.
607  * A write access to 0x80000000 will match PMP1. However we cannot cache the
608  * translation result in the TLB since this will make the write access to
609  * 0x80000008 bypass the check of PMP0.
610  * To avoid this we return a size of 1 (which means no caching) if the PMP
611  * region only covers partial of the TLB page.
612  */
613 target_ulong pmp_get_tlb_size(CPURISCVState *env, target_ulong addr)
614 {
615     target_ulong pmp_sa;
616     target_ulong pmp_ea;
617     target_ulong tlb_sa = addr & ~(TARGET_PAGE_SIZE - 1);
618     target_ulong tlb_ea = tlb_sa + TARGET_PAGE_SIZE - 1;
619     int i;
620 
621     /*
622      * If PMP is not supported or there are no PMP rules, the TLB page will not
623      * be split into regions with different permissions by PMP so we set the
624      * size to TARGET_PAGE_SIZE.
625      */
626     if (!riscv_cpu_cfg(env)->pmp || !pmp_get_num_rules(env)) {
627         return TARGET_PAGE_SIZE;
628     }
629 
630     for (i = 0; i < MAX_RISCV_PMPS; i++) {
631         if (pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg) == PMP_AMATCH_OFF) {
632             continue;
633         }
634 
635         pmp_sa = env->pmp_state.addr[i].sa;
636         pmp_ea = env->pmp_state.addr[i].ea;
637 
638         /*
639          * Only the first PMP entry that covers (whole or partial of) the TLB
640          * page really matters:
641          * If it covers the whole TLB page, set the size to TARGET_PAGE_SIZE,
642          * since the following PMP entries have lower priority and will not
643          * affect the permissions of the page.
644          * If it only covers partial of the TLB page, set the size to 1 since
645          * the allowed permissions of the region may be different from other
646          * region of the page.
647          */
648         if (pmp_sa <= tlb_sa && pmp_ea >= tlb_ea) {
649             return TARGET_PAGE_SIZE;
650         } else if ((pmp_sa >= tlb_sa && pmp_sa <= tlb_ea) ||
651                    (pmp_ea >= tlb_sa && pmp_ea <= tlb_ea)) {
652             return 1;
653         }
654     }
655 
656     /*
657      * If no PMP entry matches the TLB page, the TLB page will also not be
658      * split into regions with different permissions by PMP so we set the size
659      * to TARGET_PAGE_SIZE.
660      */
661     return TARGET_PAGE_SIZE;
662 }
663 
664 /*
665  * Convert PMP privilege to TLB page privilege.
666  */
667 int pmp_priv_to_page_prot(pmp_priv_t pmp_priv)
668 {
669     int prot = 0;
670 
671     if (pmp_priv & PMP_READ) {
672         prot |= PAGE_READ;
673     }
674     if (pmp_priv & PMP_WRITE) {
675         prot |= PAGE_WRITE;
676     }
677     if (pmp_priv & PMP_EXEC) {
678         prot |= PAGE_EXEC;
679     }
680 
681     return prot;
682 }
683