Skip to content

All Freescale macros for memory access replaced #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 31, 2015
Merged

All Freescale macros for memory access replaced #6

merged 5 commits into from
Jul 31, 2015

Conversation

AlessandroA
Copy link

tl;dr

Brace yourselves: This is a PR changing ~5k lines. Every Freescale macro containing direct memory accesses (*addr = val) has been replaced with a memory access macro (ADDRESS_WRITE(addr, val)). In this way memory accesses can be centrally redefined for special requirements. A script was used, with the help of 18 regular expressions.

Background

In uVisor we use ACLs to tell if a resource can be granted access to. Peripheral ACLs specify permission levels for whole peripheral slots, but some registers in those slots may still enforce higher restrictions. For example, RTC registers require superuser access unless a certain RTC-specific bit is set.

Accessing these registers might incur in a bus fault. uVisor is able to recover from bus faults, perform the r/w operation if an ACL covers it, and continue execution regularly, but this requires the memory access to be well defined, that is, parse-able.

The Problem

All functions in the Freescale K64F HAL ultimately use macros to perform r/w operations on peripheral registers. These macros have usually a structure like the following:

#define BW_PERIPH_REG_BIT(x, n, v) (BITBAND_ACCESS32(x, n) = (v))

where v is the value to write to the bit BIT in the register REG of the peripheral PERIPH at the address x. BITBAND_ACCESS(x, n) is a macro that de-references the aliased bitband address obtained from x and n.

With this style we are not able to centrally re-define different behaviours for memory accesses. Every single macro implements a memory access explicitly (*addr = val), which cannot be replaced by a uVisor counterpart.

The Solution

The solution is to replace every direct access to memory in the header files with a macro that implements memory access indirectly, so that if uVisor is used it turns into a dedicated function, otherwise the straightforward pointer de-referencing is used.

This is done using a script, uvisor_replace.py, that scans header files using a total of 18 regular expressions. These capture all the combinations of possible macros, classified as follows:

  • regular read/write (8, 16, 32 bits)
  • bitbanded read/write (8, 16, 32 bits)
  • union bitfield read (Freescale specifc)

Why Do I Care?

If you only use Freescale K64F HAL functions (or higher level functions that call HAL functions) you are fine - this all happens under the hood. If, instead, you sometimes access registers or their bitfields singularly with a direct memory access then you have 3 options:

  1. Use HAL functions; if you are already using some of them, this is anyway suggested for style consistency;
  2. Use macros underlying the HAL functions: B{W,R}_PERIPH_REG_BIT(x, n, v) (Freescale specific)
  3. Use memory access macros: ADDRESS_{READ,WRITE}{32,16,8}(addr(, val)); the addr field can also be replaced by a bitband translation with BITBAND_ADDRESS{32,16,8}(addr, bit) (uVisor specific)

Changelog

  • Added script to parse header and replace macros as described
  • Applied script to all header files in mbed-hal-k64f/device/MK64F12/*.h
  • Updated file mbed-hal-k64f/device/MK64F12/fsl_bitaccess.h with centralised memory access macro
  • Removed file mbed-hal-k64f/device/MK64F12/MK64F_sim_uvisor.h, superseded by these changes

- so far SIM registers were written to in unprivileged mode only by reverting to
  an SVCall; hence we needed to redefine some symbols here
- the SVCall workaround will be replaced with a more consistent set of changes
  (coming soon), so removing it
- BITBAND_ACCESSXX(x, v) macros now use another macro to calculate the bitband
  alias and then write to its location
- in this way the BITBAND_ADDRESSXX macros can be used somewhere else
- also added fallback macros for regular read/write to memory locations
  (used bu uvisor to re-define how memory accesses are performed)
@AlessandroA
Copy link
Author

@meriac @bogdanm @0xc0170

@bogdanm
Copy link
Contributor

bogdanm commented Jul 30, 2015

After carefully checking each line of this PR twice, +1

@meriac
Copy link
Contributor

meriac commented Jul 30, 2015

+1

- most of Freescale pre-processor macros have embedded memory access statements
  like the one below:
`#define BW_SOME_REGISTER_BIT(r, b, v) ( *(BITBAND_ACCESS32(r, b)) = (v) )`
  that are really hard to turn into something different. For example, with
uvisor the regular memory access is replaced by something that is easier to
check for security assertions
- the script here transforms all Freescale macros for read/write to peripheral
  registers into uvisor-friendly macros like the one below:
`#define BW_SOME_REGISTER_BIT(r, b, v) (ADDRESS_WRITE32(BITBAND_ADDRESS32(r, b), v))`
- backward compatibility is ensured with ADDRESS_WRITE32 being replace-able by
  any uvisor-agnostic macro
- use the script with `python uvisor_replace.py MK*.h`
- the script from the previous commit used to replace all the macros of the
  following classes:
    - regular read (of a whole register, of a single bit field, of a multi-bit
      field)
    - regular write (of a whole register, of a single bit field, of a multi-bit)
    - bitband read/write for byte, halfword, word
@0xc0170
Copy link
Contributor

0xc0170 commented Jul 31, 2015

+1, going to merge. Thanks @AlessandroA

Just a note for reference, if we ever update to newer KSDK (from 1.0 which we use currently to 1.2) - they changed "slightly" all these headers, and API which uses them.

0xc0170 added a commit that referenced this pull request Jul 31, 2015
All Freescale macros for memory access replaced
@0xc0170 0xc0170 merged commit cccbc69 into ARMmbed:master Jul 31, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants