Re: [PATCH 7/7] rtems: Add rtems_task_build()

2020-08-22 Thread Chris Johns
On 21/8/20 9:51 pm, Sebastian Huber wrote:
> In contrast to rtems_task_create() this function creates a task with a
> user-provided task storage area.

The name is build but it creates a task? I am wondering about
rtems_task_create_static or something along this line?

> Close #3959.

Sorry, I had not read the ticket's details.

Chris

> ---
>  cpukit/Makefile.am |   1 +
>  cpukit/include/rtems/rtems/tasks.h |  65 ++
>  cpukit/include/rtems/rtems/tasksimpl.h |  11 +
>  cpukit/rtems/src/taskbuild.c   | 273 
>  cpukit/rtems/src/taskcreate.c  | 274 +
>  testsuites/sptests/sp01/init.c |  22 +-
>  testsuites/sptests/sp01/sp01.doc   |   1 +
>  testsuites/sptests/sp01/system.h   |   2 +-
>  8 files changed, 413 insertions(+), 236 deletions(-)
>  create mode 100644 cpukit/rtems/src/taskbuild.c
> 
> diff --git a/cpukit/Makefile.am b/cpukit/Makefile.am
> index bc56822cf8..e382478eac 100644
> --- a/cpukit/Makefile.am
> +++ b/cpukit/Makefile.am
> @@ -785,6 +785,7 @@ librtemscpu_a_SOURCES += rtems/src/statustext.c
>  librtemscpu_a_SOURCES += rtems/src/statustoerrno.c
>  librtemscpu_a_SOURCES += rtems/src/systemeventreceive.c
>  librtemscpu_a_SOURCES += rtems/src/systemeventsend.c
> +librtemscpu_a_SOURCES += rtems/src/taskbuild.c
>  librtemscpu_a_SOURCES += rtems/src/taskcreate.c
>  librtemscpu_a_SOURCES += rtems/src/taskdelete.c
>  librtemscpu_a_SOURCES += rtems/src/taskexit.c
> diff --git a/cpukit/include/rtems/rtems/tasks.h 
> b/cpukit/include/rtems/rtems/tasks.h
> index 12c323e60e..dff811686a 100644
> --- a/cpukit/include/rtems/rtems/tasks.h
> +++ b/cpukit/include/rtems/rtems/tasks.h
> @@ -164,6 +164,71 @@ rtems_status_code rtems_task_create(
>rtems_id*id
>  );
>  
> +/**
> + * @brief This structure defines the configuration of a task to build with
> + *   rtems_task_build().
> + */
> +typedef struct {
> +  /**
> +   * @brief This member defines the name of the task.
> +   */
> +  rtems_name name;
> +
> +  /**
> +   * @brief This member defines initial priority of the task.
> +   */
> +  rtems_task_priority initial_priority;
> +
> +  /**
> +   * @brief This member shall point to the task storage area begin.
> +   *
> +   * The task storage area will contain the task stack, the thread-local
> +   * storage, and, on some architectures, the floating-point context.
> +   */
> +  void *storage_area;
> +
> +  /**
> +   * @brief This member defines size of the task storage area in bytes.
> +   */
> +  size_t storage_size;
> +
> +  /**
> +   * @brief This member defines the optional handler to free the task storage
> +   *   area.
> +   *
> +   * It may be NULL.
> +   */
> +  void ( *storage_free )( void * );

Called under what context? What can this call do? For example call the standard
heap allocator.

Chris

> +
> +  /**
> +   * @brief This member defines the initial modes of the task.
> +   */
> +  rtems_mode initial_modes;
> +
> +  /**
> +   * @brief This member defines the attributes of the task.
> +   */
> +  rtems_attribute attributes;
> +} rtems_task_config;
> +
> +/**
> + * @brief Builds a task according to the specified configuration.
> + *
> + * @param config The task configuration.
> + * @param[out] id The task identifier of the new task.
> + *
> + * @retval RTEMS_SUCCESSFUL Successful operation.
> + * @retval RTEMS_INVALID_ADDRESS The id parameter is @c NULL.
> + * @retval RTEMS_INVALID_NAME The task name is invalid.
> + * @retval RTEMS_INVALID_PRIORITY The initial priority of the task is 
> invalid.
> + * @retval RTEMS_TOO_MANY No task is available.
> + * @retval RTEMS_UNSATISFIED A task create extension failed.
> + */
> +rtems_status_code rtems_task_build(
> +  const rtems_task_config *config,
> +  rtems_id*id
> +);
> +
>  /**
>   * @brief RTEMS Task Name to Id
>   *
> diff --git a/cpukit/include/rtems/rtems/tasksimpl.h 
> b/cpukit/include/rtems/rtems/tasksimpl.h
> index c9544f8c27..b1c1f38899 100644
> --- a/cpukit/include/rtems/rtems/tasksimpl.h
> +++ b/cpukit/include/rtems/rtems/tasksimpl.h
> @@ -42,6 +42,17 @@ extern "C" {
>   */
>  void _RTEMS_tasks_Initialize_user_tasks( void );
>  
> +typedef void ( *RTEMS_tasks_Prepare_stack )(
> +  Thread_Configuration *,
> +  const rtems_task_config *
> +);
> +
> +rtems_status_code _RTEMS_tasks_Build(
> +  const rtems_task_config   *config,
> +  rtems_id  *id,
> +  RTEMS_tasks_Prepare_stack  prepare_stack
> +);
> +
>  RTEMS_INLINE_ROUTINE Thread_Control *_RTEMS_tasks_Allocate(void)
>  {
>_Objects_Allocator_lock();
> diff --git a/cpukit/rtems/src/taskbuild.c b/cpukit/rtems/src/taskbuild.c
> new file mode 100644
> index 00..28f9f536f6
> --- /dev/null
> +++ b/cpukit/rtems/src/taskbuild.c
> @@ -0,0 +1,273 @@
> +/**
> + *  @file
> + *
> + *  @brief RTEMS Task Create
> + *  @ingroup ClassicTasks
> + */
> +
> +/*
> + *  COPYRIGHT (c) 1989-2014,2016.
> + *  On-Line Applications Research Corporation (OAR).
> 

Re: [PATCH 0/1] Adds QEP driver to Beaglebone BSP

2020-08-22 Thread Chris Johns
Hi James,

Thank you for the patch and adding this functionality.

On 21/8/20 8:01 pm, James Fitzsimons wrote:
> This patch adds driver support for the eQEP (enhanced Quadrature Encoder 
> Pulse) 
> module within each of the PWMSS units in the AM335x. The eQEP module is used 
> for 
> hardware decoding of rotrary encoders, motor encoders etc. 
> 
> Because the PWMSS module includes several components some of the existing 
> code in
> the pwm driver could be reused. To make this common I have added a pwmss.h 
> header
> and moved some of the pwmss specific defines and enum to this file. The 
> pwmss.c
> file contains a refactored (simplified) version of the clock configuration
> method that was previously in the pwm.c file. The pwmss_module_clk_config will
> now be shared by both the pwm and eqep drivers (and eventually the ecap 
> driver if
> that is ever added).
> 
> The approach taken with the qep.h header was to move some of the qep specific 
> defines from the am335x.h header to this file. They are specific to the qep 
> function and would likely never be referenced by anything other than this 
> driver.
> Doing this keeps these definitions with the driver code and reduces clutter 
> in 
> am335x.h header.
> 
> The driver supports two primary modes of operation. A polled mode (which is 
> the
> default mode), and an interrupt event driven mode.
> 
> The one area of the patch that I am least happy with and could do with some 
> advice on 
> is the callback approach I have used for the interrupt event driven model. 
> Here a 
> unit timer is configured with a set time period (in nanoseconds) and on 
> expiry the unit
> timer raises an interrupt. The interrupt handler calls an optional callback 
> function that
> the user can supply when setting the unit timer period via the 
> beagle_eqep_set_timer_period. Although this works ok, I realise that calling 
> a user 
> function from a OS level interrupt is not desirable. Better suggestions for 
> this would be
> gratefully received.

I am fine with the callback happening in the interrupt context if it is clearly
documented. Users are free to implement any event or thread sync model they 
want.

Chris

> 
> I will send a follow up email including some sample test programs with their 
> associated 
> output.
> 
> Regards,
> James
> 
> James Fitzsimons (1):
>   Adding QEP driver to Beaglebone bsp
> 
>  bsps/arm/beagle/headers.am  |   2 +
>  bsps/arm/beagle/include/bsp/bbb-pwm.h   |  11 -
>  bsps/arm/beagle/include/bsp/pwmss.h |  54 +++
>  bsps/arm/beagle/include/bsp/qep.h   | 331 ++
>  bsps/arm/beagle/pwm/pwm.c   |  64 +---
>  bsps/arm/beagle/pwmss/pwmss.c   |  64 
>  bsps/arm/beagle/qep/qep.c   | 435 
>  c/src/lib/libbsp/arm/beagle/Makefile.am |   6 +
>  8 files changed, 896 insertions(+), 71 deletions(-)
>  create mode 100644 bsps/arm/beagle/include/bsp/pwmss.h
>  create mode 100644 bsps/arm/beagle/include/bsp/qep.h
>  create mode 100644 bsps/arm/beagle/pwmss/pwmss.c
>  create mode 100644 bsps/arm/beagle/qep/qep.c
> 
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


Re: [PATCH 1/1] Adding QEP driver to Beaglebone bsp

2020-08-22 Thread Chris Johns
On 21/8/20 8:01 pm, James Fitzsimons wrote:
> ---
>  bsps/arm/beagle/headers.am  |   2 +
>  bsps/arm/beagle/include/bsp/bbb-pwm.h   |  11 -
>  bsps/arm/beagle/include/bsp/pwmss.h |  54 +++
>  bsps/arm/beagle/include/bsp/qep.h   | 331 ++
>  bsps/arm/beagle/pwm/pwm.c   |  64 +---
>  bsps/arm/beagle/pwmss/pwmss.c   |  64 
>  bsps/arm/beagle/qep/qep.c   | 435 
>  c/src/lib/libbsp/arm/beagle/Makefile.am |   6 +
>  8 files changed, 896 insertions(+), 71 deletions(-)
>  create mode 100644 bsps/arm/beagle/include/bsp/pwmss.h
>  create mode 100644 bsps/arm/beagle/include/bsp/qep.h
>  create mode 100644 bsps/arm/beagle/pwmss/pwmss.c
>  create mode 100644 bsps/arm/beagle/qep/qep.c
> 
> diff --git a/bsps/arm/beagle/headers.am b/bsps/arm/beagle/headers.am
> index 4dc35f2e2a..e4a746b2e1 100644
> --- a/bsps/arm/beagle/headers.am
> +++ b/bsps/arm/beagle/headers.am
> @@ -13,3 +13,5 @@ include_bsp_HEADERS += 
> ../../../../../../bsps/arm/beagle/include/bsp/beagleboneb
>  include_bsp_HEADERS += ../../../../../../bsps/arm/beagle/include/bsp/i2c.h
>  include_bsp_HEADERS += ../../../../../../bsps/arm/beagle/include/bsp/irq.h
>  include_bsp_HEADERS += ../../../../../../bsps/arm/beagle/include/bsp/spi.h
> +include_bsp_HEADERS += ../../../../../../bsps/arm/beagle/include/bsp/qep.h
> +include_bsp_HEADERS += ../../../../../../bsps/arm/beagle/include/bsp/pwmss.h
> diff --git a/bsps/arm/beagle/include/bsp/bbb-pwm.h 
> b/bsps/arm/beagle/include/bsp/bbb-pwm.h
> index cf5d6fe552..6fdba486be 100644
> --- a/bsps/arm/beagle/include/bsp/bbb-pwm.h
> +++ b/bsps/arm/beagle/include/bsp/bbb-pwm.h
> @@ -31,17 +31,6 @@ extern "C" {
>  #define BBB_CONTROL_CONF_GPMC_AD(n)   (0x800 + (n * 4))
>  #define BBB_CONTROL_CONF_LCD_DATA(n)   (0x8a0 + (n * 4))
>  
> -/**
> - * @brief The set of possible PWM subsystem module
> - *
> - * Enumerated type to define various instance of pwm module.
> - */
> -typedef enum{
> -  BBB_PWMSS0 = 0,
> -  BBB_PWMSS1,
> -  BBB_PWMSS2,
> -  BBB_PWMSS_COUNT
> -}BBB_PWMSS;
>  
>  typedef enum{
>BBB_P8_13_2B = 3,
> diff --git a/bsps/arm/beagle/include/bsp/pwmss.h 
> b/bsps/arm/beagle/include/bsp/pwmss.h
> new file mode 100644
> index 00..3a9dcbb70a
> --- /dev/null
> +++ b/bsps/arm/beagle/include/bsp/pwmss.h
> @@ -0,0 +1,54 @@
> +/**
> + * @file
> + *
> + * @ingroup arm_beagle
> + *
> + * @brief Shared PWMSS module functions used by PWM, eQEP and eCAP (when 
> added).
> + */
> +
> +/**
> + * Copyright (c) 2020 James Fitzsimons 
> + *
> + * The license and distribution terms for this file may be
> + * found in the file LICENSE in this distribution or at
> + * http://www.rtems.org/license/LICENSE.
> + *.
> + */
> +
> +#ifndef LIBBSP_ARM_BEAGLE_PWMSS_H
> +#define LIBBSP_ARM_BEAGLE_PWMSS_H
> +
> +#ifdef __cplusplus
> +extern "C" {
> +#endif /* __cplusplus */
> +
> +/* The following definitions are bitmasks for the clk control registers for
> + * the PWMSS module clocks. All three modules have the same clock control
> + * hence the EPMSSx to signify these values are consistent across all
> + * EPWMSS instances. */
> +#define AM335X_CM_PER_EPWMSSx_CLKCTRL_MODULEMODE_ENABLE   (0x2u)
> +#define AM335X_CM_PER_EPWMSSx_CLKCTRL_MODULEMODE   (0x0003u)
> +#define AM335X_CM_PER_EPWMSSx_CLKCTRL_IDLEST_FUNC  (0x0u)
> +#define AM335X_CM_PER_EPWMSSx_CLKCTRL_IDLEST_SHIFT (0x0010u)
> +#define AM335X_CM_PER_EPWMSSx_CLKCTRL_IDLEST   (0x0003u)
> +
> +/**
> + * @brief The set of possible PWM subsystem module
> + *
> + * Enumerated type to define various instance of pwm module.
> + */
> +typedef enum {
> +  BBB_PWMSS0 = 0,
> +  BBB_PWMSS1,
> +  BBB_PWMSS2,
> +  BBB_PWMSS_COUNT
> +} BBB_PWMSS;
> +
> +
> +rtems_status_code pwmss_module_clk_config(BBB_PWMSS pwmss_id);
> +
> +#ifdef __cplusplus
> +}
> +#endif /* __cplusplus */
> +
> +#endif /* LIBBSP_ARM_BEAGLE_PWMSS_H */
> diff --git a/bsps/arm/beagle/include/bsp/qep.h 
> b/bsps/arm/beagle/include/bsp/qep.h
> new file mode 100644
> index 00..671d9cca6c
> --- /dev/null
> +++ b/bsps/arm/beagle/include/bsp/qep.h
> @@ -0,0 +1,331 @@
> +/**
> + * @file
> + *
> + * @ingroup arm_beagle
> + *
> + * @brief eQEP (enhanced Quadrature Encoder Pulse) support API.
> + */
> +
> +/**
> + * Copyright (c) 2020 James Fitzsimons 
> + *
> + * The license and distribution terms for this file may be
> + * found in the file LICENSE in this distribution or at
> + * http://www.rtems.org/license/LICENSE.
> + *
> + * For details of the Enhanced Quadrature Encoder Pulse (eQEP) Module refer 
> to
> + * page 2511 of the TI Technical Reference Manual
> + * (https://www.ti.com/lit/ug/spruh73q/spruh73q.pdf)
> + *
> + * This driver supports using the QEP modules in Quadrature-clock Mode.
> + * Direction-count Mode is not currently supported. Similarly the QEPI: Index
> + * or Zero Marker and QEPS: Strobe Input pins are not currently supported.
> + *
> + * The mode can be any one of:
> + *  - Quadrature-count mode

Re: [PATCH v2] bsps/shared/ofw: Implement RTEMS OFW interface

2020-08-22 Thread Gedare Bloom
On Fri, Aug 21, 2020 at 11:26 AM Niteesh G. S.  wrote:
>
>
>
> On Fri, Aug 21, 2020 at 9:38 PM Gedare Bloom  wrote:
>>
>> Hi Niteesh,
>>
>> Can you explain how you developed the new code in a way that ensures
>> that we do not have any of the previously 4-BSD licensed code left?
>
> The FDT implementation of the OF interface present is ofw_fdt.c is, 
> fortunately,
> BSD-2 licensed so I was able to copy/reuse most of it for RTEMS ofw.c
>
> The interface(openfirm.h) and the implementation(opefirm.c) that calls the
> respective KOBJs functions are BSD-4 licensed. Since it was mentioned that
> copying the interface is fine I don't think there is an issue with it. The OF
> interface in RTEMS is present in ofw_compat.h
>
> I also want to make note of one thing, there are few functions in ofw.c
> which look exactly the same as the ones present in openfirm.c can you
> please make sure that these functions won't cause any issues.
> Few examples are, rtems_ofw_get_enc_prop_alloc, 
> rtems_ofw_get_enc_prop_alloc_multi.
>
The similarity in these functions is problematic.

I still think it would be worth the trouble to try to dig through the
history to find exactly what code was introduced with the 4-bsd. I
would guess that came out of netbsd.

> Thanks,
> Niteesh.
>
>>
>> Gedare
>>
>> On Thu, Aug 20, 2020 at 1:52 AM G S Niteesh Babu  
>> wrote:
>> >
>> > RTEMS OFW is a FDT only implementation of the OpenFirmWare
>> > interface. This API is created to be compatible with FreeBSD
>> > OpenFirmWare interface. The main intention is to make
>> > porting of FreeBSD drivers to RTEMS easier.
>> >
>> > Most functions implemented have an direct one-one mapping
>> > with the original OFW API and some extra auxiliary functions
>> > were implemented to make working with device trees easier in
>> > RTEMS.
>> >
>> > Update #3784
>> > ---
>> >  bsps/include/ofw/ofw.h| 548 +++
>> >  bsps/include/ofw/ofw_compat.h |  74 
>> >  bsps/shared/ofw/ofw.c | 670 ++
>> >  spec/build/bsps/obj.yml   |   4 +
>> >  4 files changed, 1296 insertions(+)
>> >  create mode 100644 bsps/include/ofw/ofw.h
>> >  create mode 100644 bsps/include/ofw/ofw_compat.h
>> >  create mode 100644 bsps/shared/ofw/ofw.c
>> >
>> > diff --git a/bsps/include/ofw/ofw.h b/bsps/include/ofw/ofw.h
>> > new file mode 100644
>> > index 00..50f64ff695
>> > --- /dev/null
>> > +++ b/bsps/include/ofw/ofw.h
>> > @@ -0,0 +1,548 @@
>> > +/* SPDX-License-Identifier: BSD-2-Clause */
>> > +
>> > +/**
>> > + * @file
>> > + *
>> > + * @ingroup ofw
>> > + *
>> > + * RTEMS FDT implementation of OpenFirmWare Interface.
>> > + *
>> > + * RTEMS OFW is a FDT only implementation of the OpenFirmWare interface.
>> > + * This API is created to be compatible with FreeBSD OpenFirmWare 
>> > interface.
>> > + * The main intention is to make porting of FreeBSD drivers to RTEMS 
>> > easier.
>> > + */
>> > +
>> > +/*
>> > + * Copyright (C) 2020 Niteesh Babu G S 
>> > + *
>> > + * Redistribution and use in source and binary forms, with or without
>> > + * modification, are permitted provided that the following conditions
>> > + * are met:
>> > + * 1. Redistributions of source code must retain the above copyright
>> > + *notice, this list of conditions and the following disclaimer.
>> > + * 2. Redistributions in binary form must reproduce the above copyright
>> > + *notice, this list of conditions and the following disclaimer in the
>> > + *documentation and/or other materials provided with the distribution.
>> > + *
>> > + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
>> > "AS IS"
>> > + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
>> > THE
>> > + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
>> > PURPOSE
>> > + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
>> > BE
>> > + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
>> > + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
>> > + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
>> > BUSINESS
>> > + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
>> > + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
>> > + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
>> > THE
>> > + * POSSIBILITY OF SUCH DAMAGE.
>> > + */
>> > +
>> > +#ifndef _OFW_H
>> > +#define _OFW_H
>> > +
>> > +#ifdef __cplusplus
>> > +extern "C" {
>> > +#endif
>> > +
>> > +#include 
>> > +#include 
>> > +#include 
>> > +
>> > +/**
>> > + * Represents a node in the device tree. The offset from fdtp to node is 
>> > used
>> > + * instead of fdt offset to make sure this is compatible with OF 
>> > interface in
>> > + * FreeBSD.
>> > + */
>> > +typedef uint32_t  phandle_t;
>> > +/**
>> > + * Represents the effective phandle of the node.
>> > + */
>> > +typedef uint3

[PATCH v4 2/3] High level configuration option for thread stack protection

2020-08-22 Thread Utkarsh Rai
 -Configurations options are provided by using the new-build system. Refer to 
https://ftp.rtems.org/pub/rtems/people/sebh/user.pdf,
 chapter 7,for basic setup and 
https://ftp.rtems.org/pub/rtems/people/sebh/eng.pdf(chapter 9) for the 
internals of the build system.
---
 .../arm/realview-pbx-a9/bsprealviewpbxa9.yml  |  1 +
 spec/build/cpukit/cpuopts.yml |  2 ++
 spec/build/cpukit/librtemscpu.yml |  3 +++
 .../build/cpukit/optthreadstackprotection.yml | 16 
 spec/build/testsuites/samples/grp.yml |  2 ++
 .../samples/threadstackprotection.yml | 19 +++
 6 files changed, 43 insertions(+)
 create mode 100644 spec/build/cpukit/optthreadstackprotection.yml
 create mode 100644 spec/build/testsuites/samples/threadstackprotection.yml

diff --git a/spec/build/bsps/arm/realview-pbx-a9/bsprealviewpbxa9.yml 
b/spec/build/bsps/arm/realview-pbx-a9/bsprealviewpbxa9.yml
index 2721152b93..f7d2187c66 100644
--- a/spec/build/bsps/arm/realview-pbx-a9/bsprealviewpbxa9.yml
+++ b/spec/build/bsps/arm/realview-pbx-a9/bsprealviewpbxa9.yml
@@ -57,6 +57,7 @@ links:
 source:
 - bsps/arm/realview-pbx-a9/console/console-config.c
 - bsps/arm/realview-pbx-a9/console/console-polled.c
+- bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c
 - bsps/arm/realview-pbx-a9/start/bspreset.c
 - bsps/arm/realview-pbx-a9/start/bspstart.c
 - bsps/arm/realview-pbx-a9/start/bspstarthooks.c
diff --git a/spec/build/cpukit/cpuopts.yml b/spec/build/cpukit/cpuopts.yml
index 1902e543ca..f7641a5e50 100644
--- a/spec/build/cpukit/cpuopts.yml
+++ b/spec/build/cpukit/cpuopts.yml
@@ -63,6 +63,8 @@ links:
   uid: optszoff
 - role: build-dependency
   uid: optsztime
+- role: build-dependency
+  uid: optthreadstackprotection
 - role: build-dependency
   uid: optversion
 target: cpukit/include/rtems/score/cpuopts.h
diff --git a/spec/build/cpukit/librtemscpu.yml 
b/spec/build/cpukit/librtemscpu.yml
index 63a0b7dca5..9855401665 100644
--- a/spec/build/cpukit/librtemscpu.yml
+++ b/spec/build/cpukit/librtemscpu.yml
@@ -353,6 +353,7 @@ install:
   - cpukit/include/rtems/score/isrlevel.h
   - cpukit/include/rtems/score/isrlock.h
   - cpukit/include/rtems/score/memory.h
+  - cpukit/include/rtems/score/memoryprotection.h
   - cpukit/include/rtems/score/mpci.h
   - cpukit/include/rtems/score/mpciimpl.h
   - cpukit/include/rtems/score/mppkt.h
@@ -405,6 +406,7 @@ install:
   - cpukit/include/rtems/score/smplockstats.h
   - cpukit/include/rtems/score/smplockticket.h
   - cpukit/include/rtems/score/stack.h
+  - cpukit/include/rtems/score/stackprotection.h
   - cpukit/include/rtems/score/stackimpl.h
   - cpukit/include/rtems/score/states.h
   - cpukit/include/rtems/score/statesimpl.h
@@ -1509,6 +1511,7 @@ source:
 - cpukit/score/src/semaphore.c
 - cpukit/score/src/smpbarrierwait.c
 - cpukit/score/src/stackallocator.c
+- cpukit/score/src/stackprotection.c
 - cpukit/score/src/threadallocateunlimited.c
 - cpukit/score/src/thread.c
 - cpukit/score/src/threadchangepriority.c
diff --git a/spec/build/cpukit/optthreadstackprotection.yml 
b/spec/build/cpukit/optthreadstackprotection.yml
new file mode 100644
index 00..4722d9f0cb
--- /dev/null
+++ b/spec/build/cpukit/optthreadstackprotection.yml
@@ -0,0 +1,16 @@
+SPDX-License-Identifier: CC-BY-SA-4.0 OR BSD-2-Clause
+actions:
+- get-boolean: null
+- env-enable: null
+- define-condition: null
+build-type: option
+copyrights:
+- Copyright (C) 2020 Utkarsh Rai (utkarsh.ra...@gmail.com)
+default: false
+default-by-variant: []
+description: |
+  Enable the thread stack protection support
+enabled-by: true
+links: []
+name: RTEMS_THREAD_STACK_PROTECTION
+type: build
diff --git a/spec/build/testsuites/samples/grp.yml 
b/spec/build/testsuites/samples/grp.yml
index c7591dc551..3fb8d588a6 100644
--- a/spec/build/testsuites/samples/grp.yml
+++ b/spec/build/testsuites/samples/grp.yml
@@ -40,6 +40,8 @@ links:
   uid: pppd
 - role: build-dependency
   uid: ticker
+- role: build-dependency
+  uid: threadstackprotection
 - role: build-dependency
   uid: unlimited
 type: build
diff --git a/spec/build/testsuites/samples/threadstackprotection.yml 
b/spec/build/testsuites/samples/threadstackprotection.yml
new file mode 100644
index 00..a33c53d392
--- /dev/null
+++ b/spec/build/testsuites/samples/threadstackprotection.yml
@@ -0,0 +1,19 @@
+SPDX-License-Identifier: CC-BY-SA-4.0 OR BSD-2-Clause
+build-type: test-program
+cflags: []
+copyrights:
+- Copyright (C) 2020 Utkarsh Rai (utkarsh.ra...@gmail.com)
+cppflags: []
+cxxflags: []
+enabled-by: true
+features: c cprogram
+includes: []
+ldflags: []
+links: []
+source:
+- testsuites/samples/thread_stack_protection/init.c
+stlib: []
+target: testsuites/samples/thread_stack_protection.exe
+type: build
+use-after: []
+use-before: []
-- 
2.17.1

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


[PATCH v4 3/3] Thread stack sharing

2020-08-22 Thread Utkarsh Rai
For the design details of the stack sharing mechanism please refer to the 
following post -
https://gsoc2020memoryprotection.blogspot.com/2020/08/high-level-design-and-implementation-of.html
---
 cpukit/posix/src/mmap.c   |  39 -
 cpukit/posix/src/shmopen.c|  62 +---
 .../samples/thread_stack_sharing/init.c   | 136 ++
 3 files changed, 173 insertions(+), 64 deletions(-)
 create mode 100644 testsuites/samples/thread_stack_sharing/init.c

diff --git a/cpukit/posix/src/mmap.c b/cpukit/posix/src/mmap.c
index 176c6e4fe8..1d4b65cea6 100644
--- a/cpukit/posix/src/mmap.c
+++ b/cpukit/posix/src/mmap.c
@@ -28,7 +28,27 @@
 
 #include 
 #include 
+#include 
+#include 
 
+static uint32_t mmap_flag_translate(int prot)
+{
+  int prot_read;
+  int prot_write;
+  int memory_flag;
+
+  prot_read = (prot_read & PROT_READ) == PROT_READ;
+  prot_write = (prot_write & PROT_WRITE) == PROT_WRITE;
+ 
+  if(prot_read){
+memory_flag |= ( RTEMS_READ_ONLY| RTEMS_MEMORY_CACHED );
+  }
+  if(prot_write) {
+memory_flag |= ( RTEMS_READ_WRITE | RTEMS_MEMORY_CACHED );
+  }
+
+  return memory_flag;
+}
 
 /**
  * mmap chain of mappings.
@@ -50,6 +70,9 @@ void *mmap(
   boolmap_private;
   boolis_shared_shm;
   int err;
+  uint32_t memory_flags;
+  uintptr_t shared_stack_address;
+  int status;
 
   map_fixed = (flags & MAP_FIXED) == MAP_FIXED;
   map_anonymous = (flags & MAP_ANON) == MAP_ANON;
@@ -67,7 +90,10 @@ void *mmap(
 
   /*
* We can provide read, write and execute because the memory in RTEMS does
-   * not normally have protections but we cannot hide access to memory.
+   * not normally have protections but we cannot hide access to memory. For
+   * thread-stack protection we can provide no-access option, but stacks are
+   * implicitly isolated and it makes no sense to specify no-access option for
+   * already isolated stacks.
*/
   if ( prot == PROT_NONE ) {
 errno = ENOTSUP;
@@ -292,9 +318,16 @@ void *mmap(
   free( mapping );
   return MAP_FAILED;
 }
+  /**
+* We share thread-stacks only when we have a shared memory object and map
+* shared flag set
+*/
+memory_flags = mmap_flag_translate( prot );
+status = _Stackprotection_Share_stack( mapping->addr, addr, 
len,memory_flags );
+  }
+  if(status == RTEMS_INVALID_ADDRESS ) {
+rtems_chain_append_unprotected( &mmap_mappings, &mapping->node );
   }
-
-  rtems_chain_append_unprotected( &mmap_mappings, &mapping->node );
 
   mmap_mappings_lock_release( );
 
diff --git a/cpukit/posix/src/shmopen.c b/cpukit/posix/src/shmopen.c
index 1e7fda66f8..b439592fd7 100644
--- a/cpukit/posix/src/shmopen.c
+++ b/cpukit/posix/src/shmopen.c
@@ -19,13 +19,11 @@
 #include 
 #include 
 #include 
-#include 
 
 #include 
 #include 
 
 #include 
-#include 
 #include 
 #include 
 
@@ -93,61 +91,6 @@ static int shm_ftruncate( rtems_libio_t *iop, off_t length )
   return 0;
 }
 
-static int shm_stack_ftruncate ( rtems_libio_t *iop, off_t length )
-{
- int err;
- Objects_Id id;
- Objects_Name_or_id_lookup_errors obj_err;
- Thread_Control *Control;
- ISR_lock_Context lock_context;
- size_t size;
- char *name;
- POSIX_Shm_Control *shm = iop_to_shm ( iop );
-
- name = shm->Object.name.name_p;
-
- /** We assign fixed pattern of naming for thread-stacks, and treat them 
-   *  accordingly.
-   */
-  if( strncmp( name, "/taskfs/", 8) == 0 ) {
-/**
- * Obtain the object id of the thread and then get the thread control block
- * corresponding to that id. 
- */
-obj_err = _Objects_Name_to_id_u32(
-&_POSIX_Threads_Information.Objects,
-   _Objects_Build_name( name[8], name[9], name[10], name[11]),
-RTEMS_LOCAL,
-&id
-);
-Control = _Thread_Get( id, &lock_context );
- if( Control != NULL ) {
-   shm->shm_object.handle = Control->Start.Initial_stack.area;
-   if( length != Control->Start.Initial_stack.size) {
- return ENOMEM;
-   }
- } else {
-   return ENOMEM;
- }
-  }else{
-
-  _Objects_Allocator_lock();
-
-  err = (*shm->shm_object.ops->object_resize)( &shm->shm_object, length );
-
-  if ( err != 0 ) {
-_Objects_Allocator_unlock();
-rtems_set_errno_and_return_minus_one( err );
-  }
-
-  _POSIX_Shm_Update_mtime_ctime( shm );
-
-  _Objects_Allocator_unlock();
-  return 0;
-  }
-
-}
-
 static int shm_close( rtems_libio_t *iop )
 {
   POSIX_Shm_Control *shm = iop_to_shm( iop );
@@ -363,11 +306,7 @@ static const rtems_filesystem_file_handlers_r shm_handlers 
= {
   .ioctl_h = rtems_filesystem_default_ioctl,
   .lseek_h = rtems_filesystem_default_lseek,
   .fstat_h = shm_fstat,
-  #if defined ( RTEMS_THREAD_STACK_PROTECTION )
-  .ftruncate_h = shm_stack_ftruncate,
-  #else
   .ftruncate_h = shm_ftruncate,
-  #endif
   .fsync_h = rtems_filesystem_default_fsync_or_fdatasync,
   .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync,
   .fcntl_h =

[PATCH v4 1/3] Strict thread-stack isolation

2020-08-22 Thread Utkarsh Rai
- These patches are based on the new build system.
- Allocation of  page aligned stack has been done through posix_memalign(),
  which is a hack.
---
 .../realview-pbx-a9/mmu/bsp-set-mmu-attr.c|  90 +
 bsps/shared/start/stackalloc.c|  12 +-
 cpukit/include/rtems/score/memoryprotection.h |  83 
 cpukit/include/rtems/score/stack.h|  56 
 cpukit/include/rtems/score/stackprotection.h  |  80 +++
 cpukit/posix/src/shmopen.c|  61 +
 cpukit/score/cpu/arm/cpu_asm.S|  94 +
 .../score/cpu/arm/include/rtems/score/cpu.h   |   2 +-
 cpukit/score/src/stackprotection.c| 125 ++
 cpukit/score/src/threadhandler.c  |   6 +
 .../samples/thread_stack_protection/init.c| 114 
 .../thread_stack_protection.doc   |   2 +
 .../thread_stack_protection.scn   |  20 +++
 13 files changed, 743 insertions(+), 2 deletions(-)
 create mode 100644 bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c
 create mode 100644 cpukit/include/rtems/score/memoryprotection.h
 create mode 100644 cpukit/include/rtems/score/stackprotection.h
 create mode 100644 cpukit/score/src/stackprotection.c
 create mode 100644 testsuites/samples/thread_stack_protection/init.c
 create mode 100644 
testsuites/samples/thread_stack_protection/thread_stack_protection.doc
 create mode 100644 
testsuites/samples/thread_stack_protection/thread_stack_protection.scn

diff --git a/bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c 
b/bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c
new file mode 100644
index 00..4fba1e2b22
--- /dev/null
+++ b/bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c
@@ -0,0 +1,90 @@
+#include 
+#include 
+#include 
+#include 
+#include 
+
+static uint32_t translate_flags(uint32_t attr_flags)
+{
+  uint32_t flags;
+  uint32_t memory_attribute;
+
+  if( ( attr_flags & RTEMS_READ_WRITE ) == RTEMS_READ_WRITE ) {
+flags |= ARMV7_MMU_READ_WRITE;
+  }
+  if ( ( attr_flags & RTEMS_READ_ONLY ) == RTEMS_READ_ONLY ) {
+flags |= ARMV7_MMU_READ_ONLY;
+  }
+  if ( (attr_flags & RTEMS_NO_ACCESS ) == RTEMS_NO_ACCESS ) {
+flags = 0;
+  } 
+ /*
+  * Check for memory-cache operation 
+  */
+  if( attr_flags & RTEMS_MEMORY_CACHED ) {
+flags |= ARM_MMU_SECT_TEX_0 | ARM_MMU_SECT_C | ARM_MMU_SECT_B;
+  }
+
+  return flags;
+}
+
+static rtems_status_code validate_memory( uintptr_t begin, uintptr_t end )
+{ 
+  /* We can set/unset the memory attributes of the regions between the bsp 
stack
+   * section and the workspace section only.
+   */
+  if( begin < bsp_section_stack_begin  || end > bsp_section_work_end ) {
+return RTEMS_INVALID_ADDRESS;
+  } else {
+return RTEMS_SUCCESSFUL;
+  }
+  
+}
+
+rtems_status_code _Memory_protection_Set_entries(uintptr_t begin, size_t size, 
uint32_t flags)
+{
+  uintptr_t end;
+  rtems_interrupt_level irq_level;
+  uint32_t access_flags;
+  
+  end = begin + size;
+  if( validate_memory( begin, end ) == RTEMS_SUCCESSFUL ) {
+access_flags = translate_flags(flags);
+
+/*
+ * The ARM reference manual instructs to disable all the interrupts before
+ * setting up page table entries.
+ */
+rtems_interrupt_disable(irq_level);
+arm_cp15_set_translation_table_entries(begin, end, access_flags); 
+rtems_interrupt_enable(irq_level);
+
+return RTEMS_SUCCESSFUL;
+  }
+
+  return RTEMS_INVALID_ADDRESS;
+}
+
+rtems_status_code _Memory_protection_Unset_entries(uintptr_t begin, size_t 
size)
+{
+  uint32_t access_flags;
+  uintptr_t end;
+  rtems_interrupt_level irq_level;
+  
+  if( validate_memory( begin, end ) == RTEMS_SUCCESSFUL ) {
+end = begin + size;
+access_flags = translate_flags( RTEMS_READ_ONLY );
+
+/*
+ *  The ARM reference manual instructs to disable all the interrupts before
+ * setting up page table entries.
+ */
+rtems_interrupt_disable(irq_level);
+arm_cp15_set_translation_table_entries(begin, end, access_flags); 
+rtems_interrupt_enable(irq_level);
+
+return RTEMS_SUCCESSFUL;
+  }
+
+  return RTEMS_INVALID_ADDRESS;
+}
\ No newline at end of file
diff --git a/bsps/shared/start/stackalloc.c b/bsps/shared/start/stackalloc.c
index f7cf7be0f1..bf43e51198 100644
--- a/bsps/shared/start/stackalloc.c
+++ b/bsps/shared/start/stackalloc.c
@@ -25,6 +25,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 
@@ -43,7 +44,15 @@ void bsp_stack_allocate_init(size_t stack_space_size)
 void *bsp_stack_allocate(size_t size)
 {
   void *stack = NULL;
-
+#if defined (RTEMS_THREAD_STACK_PROTECTION)
+/*
+ * This is a temporary hack, we need to use _Heap_Allocate_aligned() but the 
heap
+ * initialization for bsp_stack_heap fails as bsp_section_stack_size is 0. See
+ * bsp_stack_allocate_init().
+ */
+  posix_memalign(&stack, 4096 , size);
+  _Memory_protection_Set_entries( stack, size, ( RTEMS_READ_ONLY | 
RTEMS_MEMORY_CACHED ) );
+#else
   if (b

Re: [PATCH v2] bsps/shared/ofw: Implement RTEMS OFW interface

2020-08-22 Thread Niteesh G. S.
Hello,

I went through the history of the OF implementation in NetBSD and FreeBSD.
I have summarized the things I have found out below.

The first OF implementation seems to come from NetBSD this is actually BSD-4
licensed. FreeBSD and also other BSD have used this as the base and improved
the API. For eg functions like OF_getprop_allloc_multi, OF_getencprop were
not
present in the original OF interface but were added by FreeBSD. And also
NetBSD
doesn't use KOBJS for function dispatch but rather uses platform-specific
OF
implementation, that is FDT implementation is present under
sys/dev/fdt/fdt_openfirm.c
powerPC implementation under arch/powerpc/openfirm.c.
NetBSD openfirm.h:
https://github.com/NetBSD/src/blob/trunk/sys/dev/ofw/openfirm.h
And I think was adding the OF interface FreeBSD has added the BSD-2 license.

https://github.com/freebsd/freebsd/commit/d84d862cbd66f3f9ab4ce65fe69893664689c73b
This is the commit where they have added OF interface to FreeBSD and from
the
commit it seems like they have added the BSD-2 license with the interface
itself.
And the functions where we worry about the similarity are all FreeBSD
extension
so I guess we won't have any issue since they would come under BSD-2
license.


Thanks,
Niteesh.

On Sat, Aug 22, 2020 at 7:23 PM Gedare Bloom  wrote:

> On Fri, Aug 21, 2020 at 11:26 AM Niteesh G. S. 
> wrote:
> >
> >
> >
> > On Fri, Aug 21, 2020 at 9:38 PM Gedare Bloom  wrote:
> >>
> >> Hi Niteesh,
> >>
> >> Can you explain how you developed the new code in a way that ensures
> >> that we do not have any of the previously 4-BSD licensed code left?
> >
> > The FDT implementation of the OF interface present is ofw_fdt.c is,
> fortunately,
> > BSD-2 licensed so I was able to copy/reuse most of it for RTEMS ofw.c
> >
> > The interface(openfirm.h) and the implementation(opefirm.c) that calls
> the
> > respective KOBJs functions are BSD-4 licensed. Since it was mentioned
> that
> > copying the interface is fine I don't think there is an issue with it.
> The OF
> > interface in RTEMS is present in ofw_compat.h
> >
> > I also want to make note of one thing, there are few functions in ofw.c
> > which look exactly the same as the ones present in openfirm.c can you
> > please make sure that these functions won't cause any issues.
> > Few examples are, rtems_ofw_get_enc_prop_alloc,
> rtems_ofw_get_enc_prop_alloc_multi.
> >
> The similarity in these functions is problematic.
>
> I still think it would be worth the trouble to try to dig through the
> history to find exactly what code was introduced with the 4-bsd. I
> would guess that came out of netbsd.
>
> > Thanks,
> > Niteesh.
> >
> >>
> >> Gedare
> >>
> >> On Thu, Aug 20, 2020 at 1:52 AM G S Niteesh Babu 
> wrote:
> >> >
> >> > RTEMS OFW is a FDT only implementation of the OpenFirmWare
> >> > interface. This API is created to be compatible with FreeBSD
> >> > OpenFirmWare interface. The main intention is to make
> >> > porting of FreeBSD drivers to RTEMS easier.
> >> >
> >> > Most functions implemented have an direct one-one mapping
> >> > with the original OFW API and some extra auxiliary functions
> >> > were implemented to make working with device trees easier in
> >> > RTEMS.
> >> >
> >> > Update #3784
> >> > ---
> >> >  bsps/include/ofw/ofw.h| 548 +++
> >> >  bsps/include/ofw/ofw_compat.h |  74 
> >> >  bsps/shared/ofw/ofw.c | 670
> ++
> >> >  spec/build/bsps/obj.yml   |   4 +
> >> >  4 files changed, 1296 insertions(+)
> >> >  create mode 100644 bsps/include/ofw/ofw.h
> >> >  create mode 100644 bsps/include/ofw/ofw_compat.h
> >> >  create mode 100644 bsps/shared/ofw/ofw.c
> >> >
> >> > diff --git a/bsps/include/ofw/ofw.h b/bsps/include/ofw/ofw.h
> >> > new file mode 100644
> >> > index 00..50f64ff695
> >> > --- /dev/null
> >> > +++ b/bsps/include/ofw/ofw.h
> >> > @@ -0,0 +1,548 @@
> >> > +/* SPDX-License-Identifier: BSD-2-Clause */
> >> > +
> >> > +/**
> >> > + * @file
> >> > + *
> >> > + * @ingroup ofw
> >> > + *
> >> > + * RTEMS FDT implementation of OpenFirmWare Interface.
> >> > + *
> >> > + * RTEMS OFW is a FDT only implementation of the OpenFirmWare
> interface.
> >> > + * This API is created to be compatible with FreeBSD OpenFirmWare
> interface.
> >> > + * The main intention is to make porting of FreeBSD drivers to RTEMS
> easier.
> >> > + */
> >> > +
> >> > +/*
> >> > + * Copyright (C) 2020 Niteesh Babu G S 
> >> > + *
> >> > + * Redistribution and use in source and binary forms, with or without
> >> > + * modification, are permitted provided that the following conditions
> >> > + * are met:
> >> > + * 1. Redistributions of source code must retain the above copyright
> >> > + *notice, this list of conditions and the following disclaimer.
> >> > + * 2. Redistributions in binary form must reproduce the above
> copyright
> >> > + *notice, this list of conditions and the following disclaimer
> in the
> >> > + *documentation 

Re: [PATCH v4 1/3] Strict thread-stack isolation

2020-08-22 Thread Gedare Bloom
I have some comments below. I'm not that happy with the lack of design
discussion during the iteration of this code. While it is a little
easier to critique the code, it is also a bit wasteful because I have
to also comment on stylistic problems, and when some decisions you
make while coding end up not being correct or aren't easily understood
during code review then you might have to spend more time recoding. It
can be hard to find the balance between design and code reviews,

On Sat, Aug 22, 2020 at 9:19 AM Utkarsh Rai  wrote:
>
> - These patches are based on the new build system.
> - Allocation of  page aligned stack has been done through posix_memalign(),
>   which is a hack.
> ---
>  .../realview-pbx-a9/mmu/bsp-set-mmu-attr.c|  90 +
>  bsps/shared/start/stackalloc.c|  12 +-
>  cpukit/include/rtems/score/memoryprotection.h |  83 
>  cpukit/include/rtems/score/stack.h|  56 
>  cpukit/include/rtems/score/stackprotection.h  |  80 +++
>  cpukit/posix/src/shmopen.c|  61 +
>  cpukit/score/cpu/arm/cpu_asm.S|  94 +
>  .../score/cpu/arm/include/rtems/score/cpu.h   |   2 +-
>  cpukit/score/src/stackprotection.c| 125 ++
>  cpukit/score/src/threadhandler.c  |   6 +
>  .../samples/thread_stack_protection/init.c| 114 
>  .../thread_stack_protection.doc   |   2 +
>  .../thread_stack_protection.scn   |  20 +++
>  13 files changed, 743 insertions(+), 2 deletions(-)
>  create mode 100644 bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c
>  create mode 100644 cpukit/include/rtems/score/memoryprotection.h
>  create mode 100644 cpukit/include/rtems/score/stackprotection.h
>  create mode 100644 cpukit/score/src/stackprotection.c
>  create mode 100644 testsuites/samples/thread_stack_protection/init.c
>  create mode 100644 
> testsuites/samples/thread_stack_protection/thread_stack_protection.doc
>  create mode 100644 
> testsuites/samples/thread_stack_protection/thread_stack_protection.scn
>
> diff --git a/bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c 
> b/bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c
> new file mode 100644
> index 00..4fba1e2b22
> --- /dev/null
> +++ b/bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c
> @@ -0,0 +1,90 @@
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +static uint32_t translate_flags(uint32_t attr_flags)
> +{
> +  uint32_t flags;
> +  uint32_t memory_attribute;
> +
> +  if( ( attr_flags & RTEMS_READ_WRITE ) == RTEMS_READ_WRITE ) {
space after if

> +flags |= ARMV7_MMU_READ_WRITE;
> +  }
> +  if ( ( attr_flags & RTEMS_READ_ONLY ) == RTEMS_READ_ONLY ) {
> +flags |= ARMV7_MMU_READ_ONLY;
> +  }
> +  if ( (attr_flags & RTEMS_NO_ACCESS ) == RTEMS_NO_ACCESS ) {

RTEMS_NO_ACCESS is 0. This is always true?

> +flags = 0;
> +  }
> + /*
> +  * Check for memory-cache operation
> +  */
> +  if( attr_flags & RTEMS_MEMORY_CACHED ) {
> +flags |= ARM_MMU_SECT_TEX_0 | ARM_MMU_SECT_C | ARM_MMU_SECT_B;
> +  }
> +
> +  return flags;
> +}
> +
> +static rtems_status_code validate_memory( uintptr_t begin, uintptr_t end )
> +{
> +  /* We can set/unset the memory attributes of the regions between the bsp 
> stack
> +   * section and the workspace section only.
> +   */
> +  if( begin < bsp_section_stack_begin  || end > bsp_section_work_end ) {
ws

> +return RTEMS_INVALID_ADDRESS;
> +  } else {
> +return RTEMS_SUCCESSFUL;
> +  }
> +
> +}
> +
> +rtems_status_code _Memory_protection_Set_entries(uintptr_t begin, size_t 
> size, uint32_t flags)
> +{
> +  uintptr_t end;
> +  rtems_interrupt_level irq_level;
> +  uint32_t access_flags;
> +
> +  end = begin + size;
> +  if( validate_memory( begin, end ) == RTEMS_SUCCESSFUL ) {
ws

you should also validate the flags requested for the memory region

> +access_flags = translate_flags(flags);
ws within function call parameter list, fix throughout

> +
> +/*
> + * The ARM reference manual instructs to disable all the interrupts 
> before
> + * setting up page table entries.
> + */
> +rtems_interrupt_disable(irq_level);
> +arm_cp15_set_translation_table_entries(begin, end, access_flags);
> +rtems_interrupt_enable(irq_level);
> +
> +return RTEMS_SUCCESSFUL;
> +  }
> +
> +  return RTEMS_INVALID_ADDRESS;
> +}
> +
> +rtems_status_code _Memory_protection_Unset_entries(uintptr_t begin, size_t 
> size)
> +{
> +  uint32_t access_flags;
> +  uintptr_t end;
> +  rtems_interrupt_level irq_level;
> +
> +  if( validate_memory( begin, end ) == RTEMS_SUCCESSFUL ) {
> +end = begin + size;
> +access_flags = translate_flags( RTEMS_READ_ONLY );

Shouldn't this be NONE?

> +
> +/*
> + *  The ARM reference manual instructs to disable all the interrupts 
> before
> + * setting up page table entries.
> + */
> +rtems_interrupt_disable(irq_level);
> +arm_cp15_set_translation_ta

Re: [PATCH v4 3/3] Thread stack sharing

2020-08-22 Thread Gedare Bloom
On Sat, Aug 22, 2020 at 9:19 AM Utkarsh Rai  wrote:
>
> For the design details of the stack sharing mechanism please refer to the 
> following post -
> https://gsoc2020memoryprotection.blogspot.com/2020/08/high-level-design-and-implementation-of.html
> ---
>  cpukit/posix/src/mmap.c   |  39 -
>  cpukit/posix/src/shmopen.c|  62 +---
>  .../samples/thread_stack_sharing/init.c   | 136 ++
>  3 files changed, 173 insertions(+), 64 deletions(-)
>  create mode 100644 testsuites/samples/thread_stack_sharing/init.c
>
> diff --git a/cpukit/posix/src/mmap.c b/cpukit/posix/src/mmap.c
> index 176c6e4fe8..1d4b65cea6 100644
> --- a/cpukit/posix/src/mmap.c
> +++ b/cpukit/posix/src/mmap.c
> @@ -28,7 +28,27 @@
>
>  #include 
>  #include 
> +#include 
> +#include 
>
> +static uint32_t mmap_flag_translate(int prot)
> +{
> +  int prot_read;
> +  int prot_write;
> +  int memory_flag;
> +
> +  prot_read = (prot_read & PROT_READ) == PROT_READ;
> +  prot_write = (prot_write & PROT_WRITE) == PROT_WRITE;
> +
> +  if(prot_read){
ws

> +memory_flag |= ( RTEMS_READ_ONLY| RTEMS_MEMORY_CACHED );
> +  }
> +  if(prot_write) {
> +memory_flag |= ( RTEMS_READ_WRITE | RTEMS_MEMORY_CACHED );
> +  }
> +
> +  return memory_flag;
> +}
>
>  /**
>   * mmap chain of mappings.
> @@ -50,6 +70,9 @@ void *mmap(
>boolmap_private;
>boolis_shared_shm;
>int err;
> +  uint32_t memory_flags;
> +  uintptr_t shared_stack_address;
> +  int status;
>
>map_fixed = (flags & MAP_FIXED) == MAP_FIXED;
>map_anonymous = (flags & MAP_ANON) == MAP_ANON;
> @@ -67,7 +90,10 @@ void *mmap(
>
>/*
> * We can provide read, write and execute because the memory in RTEMS does
> -   * not normally have protections but we cannot hide access to memory.
> +   * not normally have protections but we cannot hide access to memory. For
> +   * thread-stack protection we can provide no-access option, but stacks are
> +   * implicitly isolated and it makes no sense to specify no-access option 
> for
> +   * already isolated stacks.
> */
>if ( prot == PROT_NONE ) {
>  errno = ENOTSUP;
> @@ -292,9 +318,16 @@ void *mmap(
>free( mapping );
>return MAP_FAILED;
>  }
> +  /**
> +* We share thread-stacks only when we have a shared memory object and map
> +* shared flag set
> +*/
> +memory_flags = mmap_flag_translate( prot );
> +status = _Stackprotection_Share_stack( mapping->addr, addr, 
> len,memory_flags );
ws

This function doesn't exist. This patch won't compile. Normally I
would stop reviewing here.

The _Stack_protection_Share_stack() function returns 0 or -1. It
doesn't return a status code.

> +  }
> +  if(status == RTEMS_INVALID_ADDRESS ) {
> +rtems_chain_append_unprotected( &mmap_mappings, &mapping->node );
>}
> -
> -  rtems_chain_append_unprotected( &mmap_mappings, &mapping->node );
>
>mmap_mappings_lock_release( );
>
> diff --git a/cpukit/posix/src/shmopen.c b/cpukit/posix/src/shmopen.c
> index 1e7fda66f8..b439592fd7 100644
> --- a/cpukit/posix/src/shmopen.c
> +++ b/cpukit/posix/src/shmopen.c
> @@ -19,13 +19,11 @@
>  #include 
>  #include 
>  #include 
> -#include 
>
>  #include 
>  #include 
>
>  #include 
> -#include 
>  #include 
>  #include 
>
> @@ -93,61 +91,6 @@ static int shm_ftruncate( rtems_libio_t *iop, off_t length 
> )
>return 0;
>  }
>
> -static int shm_stack_ftruncate ( rtems_libio_t *iop, off_t length )
> -{
> - int err;
> - Objects_Id id;
> - Objects_Name_or_id_lookup_errors obj_err;
> - Thread_Control *Control;
> - ISR_lock_Context lock_context;
> - size_t size;
> - char *name;
> - POSIX_Shm_Control *shm = iop_to_shm ( iop );
> -
> - name = shm->Object.name.name_p;
> -
> - /** We assign fixed pattern of naming for thread-stacks, and treat them
> -   *  accordingly.
> -   */
> -  if( strncmp( name, "/taskfs/", 8) == 0 ) {
> -/**
> - * Obtain the object id of the thread and then get the thread control 
> block
> - * corresponding to that id.
> - */
> -obj_err = _Objects_Name_to_id_u32(
> -&_POSIX_Threads_Information.Objects,
> -   _Objects_Build_name( name[8], name[9], name[10], name[11]),
> -RTEMS_LOCAL,
> -&id
> -);
> -Control = _Thread_Get( id, &lock_context );
> - if( Control != NULL ) {
> -   shm->shm_object.handle = Control->Start.Initial_stack.area;
> -   if( length != Control->Start.Initial_stack.size) {
> - return ENOMEM;
> -   }
> - } else {
> -   return ENOMEM;
> - }
> -  }else{
> -
> -  _Objects_Allocator_lock();
> -
> -  err = (*shm->shm_object.ops->object_resize)( &shm->shm_object, length );
> -
> -  if ( err != 0 ) {
> -_Objects_Allocator_unlock();
> -rtems_set_errno_and_return_minus_one( err );
> -  }
> -
> -  _POSIX_Shm_Update_mtime_ctime( shm );
> -
> -  _Objects_Allocator_unlock();
> -  return 0;
> -  }
> -
> -}
> -
Why are you re

[PATCH] Pre Release: Strong APA

2020-08-22 Thread Richi Dubey
---
 cpukit/include/rtems/scheduler.h  |   6 +-
 .../include/rtems/score/schedulerstrongapa.h  | 284 --
 cpukit/score/src/schedulerstrongapa.c | 920 ++
 3 files changed, 908 insertions(+), 302 deletions(-)

diff --git a/cpukit/include/rtems/scheduler.h b/cpukit/include/rtems/scheduler.h
index 955a83cfb4..b101842ba7 100644
--- a/cpukit/include/rtems/scheduler.h
+++ b/cpukit/include/rtems/scheduler.h
@@ -257,16 +257,14 @@
   #define RTEMS_SCHEDULER_STRONG_APA( name, prio_count ) \
 static struct { \
   Scheduler_strong_APA_Context Base; \
-  Chain_ControlReady[ ( prio_count ) ]; \
+  Scheduler_strong_APA_Struct Struct[ CONFIGURE_MAXIMUM_PROCESSORS ]; \
 } SCHEDULER_STRONG_APA_CONTEXT_NAME( name )
 
   #define RTEMS_SCHEDULER_TABLE_STRONG_APA( name, obj_name ) \
 { \
   &SCHEDULER_STRONG_APA_CONTEXT_NAME( name ).Base.Base.Base, \
   SCHEDULER_STRONG_APA_ENTRY_POINTS, \
-  RTEMS_ARRAY_SIZE( \
-SCHEDULER_STRONG_APA_CONTEXT_NAME( name ).Ready \
-  ) - 1, \
+  SCHEDULER_STRONG_APA_MAXIMUM_PRIORITY, \
   ( obj_name ) \
   SCHEDULER_CONTROL_IS_NON_PREEMPT_MODE_SUPPORTED( false ) \
 }
diff --git a/cpukit/include/rtems/score/schedulerstrongapa.h 
b/cpukit/include/rtems/score/schedulerstrongapa.h
index 0ac28cb439..86d91688e8 100644
--- a/cpukit/include/rtems/score/schedulerstrongapa.h
+++ b/cpukit/include/rtems/score/schedulerstrongapa.h
@@ -6,31 +6,38 @@
  * @brief Strong APA Scheduler API
  */
 
-/*
- * Copyright (c) 2013, 2018 embedded brains GmbH.  All rights reserved.
+/* 
+ * Copyright (c) 2020 Richi Dubey
  *
- *  embedded brains GmbH
- *  Dornierstr. 4
- *  82178 Puchheim
- *  Germany
- *  
+ *  
  *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
+ * Copyright (c) 2013, 2018 embedded brains GmbH. All rights reserved.
+ *
+ *  embedded brains GmbH   
+ *  Dornierstr. 4  
+ *  82178 Puchheim 
+ *  Germany
+ *   
+ * 
+ * The license and distribution terms for this file may be 
+ * found in the file LICENSE in this distribution or at
  * http://www.rtems.org/license/LICENSE.
  */
-
+ 
 #ifndef _RTEMS_SCORE_SCHEDULERSTRONGAPA_H
 #define _RTEMS_SCORE_SCHEDULERSTRONGAPA_H
 
 #include 
-#include 
 #include 
+#include 
 
 #ifdef __cplusplus
 extern "C" {
 #endif /* __cplusplus */
 
+#define STRONG_SCHEDULER_NODE_OF_CHAIN( node ) \
+  RTEMS_CONTAINER_OF( next, Scheduler_strong_APA_Node, Chain )
+
 /**
  * @defgroup RTEMSScoreSchedulerStrongAPA Strong APA Scheduler
  *
@@ -38,43 +45,98 @@ extern "C" {
  *
  * @brief Strong APA Scheduler
  *
- * This is an implementation of the global fixed priority scheduler (G-FP).  It
- * uses one ready chain per priority to ensure constant time insert operations.
- * The scheduled chain uses linear insert operations and has at most processor
- * count entries.  Since the processor and priority count are constants all
- * scheduler operations complete in a bounded execution time.
+ * This is an implementation of the Strong APA scheduler defined by
+ * Cerqueira et al. in Linux's Processor Affinity API, Refined: 
+ * Shifting Real-Time Tasks Towards Higher Schedulability.
  *
- * The the_thread preempt mode will be ignored.
+ * This is an implementation of the Strong APA scheduler defined by
+ * Cerqueira et al. in Linux's Processor Affinity API, Refined: 
+ * Shifting Real-Time Tasks Towards Higher Schedulability.
  *
  * @{
  */
 
 /**
- * @brief Scheduler context specialization for Strong APA
- * schedulers.
- */
-typedef struct {
-  Scheduler_SMP_ContextBase;
-  Priority_bit_map_Control Bit_map;
-  Chain_ControlReady[ RTEMS_ZERO_LENGTH_ARRAY ];
-} Scheduler_strong_APA_Context;
-
-/**
- * @brief Scheduler node specialization for Strong APA
- * schedulers.
+ * @brief Scheduler node specialization for Strong APA schedulers.
  */
 typedef struct {
   /**
* @brief SMP scheduler node.
*/
   Scheduler_SMP_Node Base;
+  
+ /**
+   * @brief Chain node for Scheduler_strong_APA_Context::All_nodes
+   */
+  Chain_Node Chain;
+  
+  /**
+   * @brief CPU that this node would preempt in the backtracking part of
+   * _Scheduler_strong_APA_Get_highest_ready and
+   * _Scheduler_strong_APA_Do_Enqueue.
+   */
+  Per_CPU_Control *invoker;
 
   /**
-   * @brief The associated ready queue of this node.
+   * @brief The associated affinity set of this node.
*/
-  Scheduler_priority_Ready_queue Ready_queue;
+  Processor_mask Affinity;
 } Scheduler_strong_APA_Node;
 
+
+/**
+ * @brief Struct for each index of the variable size arrays
+ */
+typedef struct
+{
+  /**
+   * @brief The node that called this CPU, i.e. a node which has
+   * the cpu at the index of Scheduler_strong_APA_Context::Struct in
+   * its affinity set.
+   */  
+  Scheduler_Node *caller;
+  
+/**
+   * @brief Cpu at the index of Scheduler_strong_APA_Context::Struct
+   * in Queue imp

Pre release version: Strong APA Scheduler

2020-08-22 Thread Richi Dubey
Hi,

I am really excited to annonunce the pre-release verion of Strong APA
Scheduer for RTEMS that we've been working so hard for!

The documentation for the scheduler is now live at :
https://richidubey.github.io/Strong-APA-Documentation/html/

To understand how the code works, please go through these links (in order):

Documentation for Struct defined for various uses

Changes for Enqueue

Changes for Get_highest_ready


Improvements over v1

(after
last  review):

- Better coding style and variable naming
- Better documentation
- Successfully runs the Strong APA test


Plans for v2:

- Use chain of scheduled nodes present in SMP Context rather than per cpu
variables based on this
 discussion
- Work on the reviews

I would request you to provide your reviews on whether the variables names
that I chose makes sense (like the array Struct) and if my code follows the
coding conventions. Also, please let me know if there's anything that's
still looks wrong in the code.

Please use this
 patch or
this  pull request to checkout
my changes.

Thanks,
Richi.
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: Pre release version: Strong APA Scheduler

2020-08-22 Thread Richi Dubey
v1 should point to the link:
https://lists.rtems.org/pipermail/devel/2020-July/060645.html

On Sun, Aug 23, 2020 at 3:27 AM Richi Dubey  wrote:

> Hi,
>
> I am really excited to annonunce the pre-release verion of Strong APA
> Scheduer for RTEMS that we've been working so hard for!
>
> The documentation for the scheduler is now live at :
> https://richidubey.github.io/Strong-APA-Documentation/html/
>
> To understand how the code works, please go through these links (in order):
>
> Documentation for Struct defined for various uses
> 
> Changes for Enqueue
> 
> Changes for Get_highest_ready
> 
>
> Improvements over v1
> 
>  (after
> last  review):
>
> - Better coding style and variable naming
> - Better documentation
> - Successfully runs the Strong APA test
> 
>
> Plans for v2:
>
> - Use chain of scheduled nodes present in SMP Context rather than per cpu
> variables based on this
> 
> discussion
> - Work on the reviews
>
> I would request you to provide your reviews on whether the variables names
> that I chose makes sense (like the array Struct) and if my code follows the
> coding conventions. Also, please let me know if there's anything that's
> still looks wrong in the code.
>
> Please use this
>  patch
> or this  pull request to
> checkout my changes.
>
> Thanks,
> Richi.
>
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [GSoC 2020]: Need help in writing sed alternative in Python for RSB recipes

2020-08-22 Thread Mritunjay Sharma
[Good news and Update]:

Thank you so much Chris! Your advice to search for macros using --trace
solved the problem of hard coding!
It took two complete days to figure out this beautiful thing but it is
every worth it.

Now the user has to just enter the below command and it will make things
work:

`../source-builder/sb-builder --with-rtems-bsp="xilinx_zynq_a9_qemu"
--log=log_epics epics-7-1  --trace --prefix=$HOME/development/rtems/5-arm`

Note: prefix will depend on the user.

What made this possible? Your suggestion and the changes in code mentioned
below:

```diff --git a/rtems/config/epics/epics-7-1.cfg
b/rtems/config/epics/epics-7-1.cfg
index aeb39a9..4b20f82 100644
--- a/rtems/config/epics/epics-7-1.cfg
+++ b/rtems/config/epics/epics-7-1.cfg
@@ -6,6 +6,8 @@
  %define release 1
 %endif

+%include %{_configdir}/rtems-bsp.cfg
+
 #
 # EPICS Version
 #
diff --git a/source-builder/config/epics-7-1.cfg
b/source-builder/config/epics-7-1.cfg
index a9581a2..a47aecb 100644
--- a/source-builder/config/epics-7-1.cfg
+++ b/source-builder/config/epics-7-1.cfg
@@ -40,7 +40,10 @@ URL:  https://epics.mpg.de/

   %{build_build_flags}

-  %{__make} PREFIX=%{_prefix} RTEMS_BASE=$HOME/development/rtems/5-arm
RTEMS_VERSION=5 CROSS_COMPILER_TARGET_ARCHS=RTEMS-xilinx_zynq_a9_qemu
+  #
+  # Using macros to dynamically path to RTEMS_BASE and RTEMS_VERSION using
--with-rtems-bsp
+  #
+  %{__make} PREFIX=%{_prefix} RTEMS_BASE=%{_exec_prefix}
RTEMS_VERSION=%{rtems_version}

   cd ${build_top}

@@ -50,6 +53,5 @@ URL:  https://epics.mpg.de/
   rm -rf $SB_BUILD_ROOT

   cd ${source_dir_epics}
-  %{__make} DESTDIR=$SB_BUILD_ROOT PREFIX=%{_prefix}
RTEMS_BASE=$HOME/development/rtems/5-arm RTEMS_VERSION=5
CROSS_COMPILER_TARGET_ARCHS=RTEMS-xilinx_zynq_a9_qemu
-
+  %{__make} DESTDIR=$SB_BUILD_ROOT PREFIX=%{_prefix}
RTEMS_BASE=%{_exec_prefix} RTEMS_VERSION=%{rtems_version}
   cd ${build_top}
```

These couple of changes made this build successful using RSB recipe. The
only problem remains
is that a warning of 'no hash found' is coming, even though I have added
it.

The above changes can be found pushed here:
https://github.com/RTEMS/rtems-source-builder/commit/a505877157f63f6ae17906276b3ffcb699ed1297

Please do give feedback and suggest improvements so that the recipe can
become mergeable.

Thanks
Mritunjay Sharma







On Thu, Aug 20, 2020 at 8:51 AM Chris Johns  wrote:

> On 20/8/20 8:09 am, Mritunjay Sharma wrote:
> > [UPDATE]: Finally modified the RSB recipes to make them work with make
> utility
> > and EPICS was built successfully.
>
> Great you have had some success but there is a lot more work before it is
> usable.
>
> > iff --git a/source-builder/config/epics-7-1.cfg
> b/source-builder/config/epics-7-1.cfg
> > index f51c6582..a9581a2e 100644
> > --- a/source-builder/config/epics-7-1.cfg
> > +++ b/source-builder/config/epics-7-1.cfg
> > @@ -21,7 +21,6 @@ URL:  https://epics.mpg.de/
> >  #
> >  %source set epics --rsb-file=epics-base-%{epics_version}.tar.gz
> https://gitlab.fhi.mpg.de/junkes/epics-base/-/archive/%{epics_version}/epics-base-%{epics_version}.tar.gz
> >
> > -
> >  #
> >  # Prepare the source code.
> >  #
> > @@ -31,20 +30,9 @@ URL: https://epics.mpg.de/
> >source_dir_epics="epics-base-%{epics_version}"
> >
> >%source setup epics -q -n epics-base-%{epics_version}
> > -#
> > -# Changing the RTEMS Version in
> epics-base/configure/os/CONFIG_SITE.Common.RTEMS
> > -#
> > -sed -i 's/RTEMS_VERSION = .*/RTEMS_VERSION = 5/g'
> configure/os/CONFIG_SITE.Common.RTEMS
> > -
> > -#
> > -# Changing the RTEMS Base in
> epics-base/configure/os/CONFIG_SITE.Common.RTEMS
> > -#
> > -sed -i "s/^RTEMS_BASE .*/RTEMS_BASE =
> \/home\/mritunjay\/development\/rtems\/\$\(RTEMS_VERSION\)\-arm/g"
> configure/os/CONFIG_SITE.Common.RTEMS
> >
> >cd ${build_top}
> >
> > -
> > -
> >  %build
> >build_top=$(pwd)
> >
> > @@ -52,7 +40,7 @@ sed -i "s/^RTEMS_BASE .*/RTEMS_BASE =
> \/home\/mritunjay\/development\/rtems\/\$\
> >
> >%{build_build_flags}
> >
> > -  %{__make} PREFIX=%{_prefix}
> > +  %{__make} PREFIX=%{_prefix} RTEMS_BASE=$HOME/development/rtems/5-arm
> RTEMS_VERSION=5 CROSS_COMPILER_TARGET_ARCHS=RTEMS-xilinx_zynq_a9_qemu
> >
> >cd ${build_top}
> >
> > @@ -62,6 +50,6 @@ sed -i "s/^RTEMS_BASE .*/RTEMS_BASE =
> \/home\/mritunjay\/development\/rtems\/\$\
> >rm -rf $SB_BUILD_ROOT
> >
> >cd ${source_dir_epics}
> > -  %{__make} DESTDIR=$SB_BUILD_ROOT PREFIX=%{_prefix} install
> > +  %{__make} DESTDIR=$SB_BUILD_ROOT PREFIX=%{_prefix}
> RTEMS_BASE=$HOME/development/rtems/5-arm RTEMS_VERSION=5
> CROSS_COMPILER_TARGET_ARCHS=RTEMS-xilinx_zynq_a9_qemu
>
> Mritunjay, can you see what is wrong here? Lets have a look ...
>
> $ stat $HOME/development/rtems/5-arm
> stat: /home/chris/development/rtems/5-arm: stat: No such file or directory
>
> This will not work for me and it will not work for an RSB vertical stack
> build.
> This last requirement is important.
>
> You cannot hard code values. The RSB provides the

Re: [PATCH v4 1/3] Strict thread-stack isolation

2020-08-22 Thread Utkarsh Rai
On Sat, Aug 22, 2020 at 11:32 PM Gedare Bloom  wrote:

> I have some comments below. I'm not that happy with the lack of design
> discussion during the iteration of this code. While it is a little
> easier to critique the code, it is also a bit wasteful because I have
> to also comment on stylistic problems, and when some decisions you
> make while coding end up not being correct or aren't easily understood
> during code review then you might have to spend more time recoding. It
> can be hard to find the balance between design and code reviews,
>
> On Sat, Aug 22, 2020 at 9:19 AM Utkarsh Rai 
> wrote:
> >
> > - These patches are based on the new build system.
> > - Allocation of  page aligned stack has been done through
> posix_memalign(),
> >   which is a hack.
> > ---
> >  .../realview-pbx-a9/mmu/bsp-set-mmu-attr.c|  90 +
> >  bsps/shared/start/stackalloc.c|  12 +-
> >  cpukit/include/rtems/score/memoryprotection.h |  83 
> >  cpukit/include/rtems/score/stack.h|  56 
> >  cpukit/include/rtems/score/stackprotection.h  |  80 +++
> >  cpukit/posix/src/shmopen.c|  61 +
> >  cpukit/score/cpu/arm/cpu_asm.S|  94 +
> >  .../score/cpu/arm/include/rtems/score/cpu.h   |   2 +-
> >  cpukit/score/src/stackprotection.c| 125 ++
> >  cpukit/score/src/threadhandler.c  |   6 +
> >  .../samples/thread_stack_protection/init.c| 114 
> >  .../thread_stack_protection.doc   |   2 +
> >  .../thread_stack_protection.scn   |  20 +++
> >  13 files changed, 743 insertions(+), 2 deletions(-)
> >  create mode 100644 bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c
> >  create mode 100644 cpukit/include/rtems/score/memoryprotection.h
> >  create mode 100644 cpukit/include/rtems/score/stackprotection.h
> >  create mode 100644 cpukit/score/src/stackprotection.c
> >  create mode 100644 testsuites/samples/thread_stack_protection/init.c
> >  create mode 100644
> testsuites/samples/thread_stack_protection/thread_stack_protection.doc
> >  create mode 100644
> testsuites/samples/thread_stack_protection/thread_stack_protection.scn
> >
> > diff --git a/bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c
> b/bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c
> > new file mode 100644
> > index 00..4fba1e2b22
> > --- /dev/null
> > +++ b/bsps/arm/realview-pbx-a9/mmu/bsp-set-mmu-attr.c
> > @@ -0,0 +1,90 @@
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> > +
> > +static uint32_t translate_flags(uint32_t attr_flags)
> > +{
> > +  uint32_t flags;
> > +  uint32_t memory_attribute;
> > +
> > +  if( ( attr_flags & RTEMS_READ_WRITE ) == RTEMS_READ_WRITE ) {
> space after if
>
> > +flags |= ARMV7_MMU_READ_WRITE;
> > +  }
> > +  if ( ( attr_flags & RTEMS_READ_ONLY ) == RTEMS_READ_ONLY ) {
> > +flags |= ARMV7_MMU_READ_ONLY;
> > +  }
> > +  if ( (attr_flags & RTEMS_NO_ACCESS ) == RTEMS_NO_ACCESS ) {
>
> RTEMS_NO_ACCESS is 0. This is always true?
>
> > +flags = 0;
> > +  }
> > + /*
> > +  * Check for memory-cache operation
> > +  */
> > +  if( attr_flags & RTEMS_MEMORY_CACHED ) {
> > +flags |= ARM_MMU_SECT_TEX_0 | ARM_MMU_SECT_C | ARM_MMU_SECT_B;
> > +  }
> > +
> > +  return flags;
> > +}
> > +
> > +static rtems_status_code validate_memory( uintptr_t begin, uintptr_t
> end )
> > +{
> > +  /* We can set/unset the memory attributes of the regions between the
> bsp stack
> > +   * section and the workspace section only.
> > +   */
> > +  if( begin < bsp_section_stack_begin  || end > bsp_section_work_end ) {
> ws
>

I bang my head every time this happens.


> > +return RTEMS_INVALID_ADDRESS;
> > +  } else {
> > +return RTEMS_SUCCESSFUL;
> > +  }
> > +
> > +}
> > +
> > +rtems_status_code _Memory_protection_Set_entries(uintptr_t begin,
> size_t size, uint32_t flags)
> > +{
> > +  uintptr_t end;
> > +  rtems_interrupt_level irq_level;
> > +  uint32_t access_flags;
> > +
> > +  end = begin + size;
> > +  if( validate_memory( begin, end ) == RTEMS_SUCCESSFUL ) {
> ws
>
> you should also validate the flags requested for the memory region
>
> > +access_flags = translate_flags(flags);
> ws within function call parameter list, fix throughout
>
> > +
> > +/*
> > + * The ARM reference manual instructs to disable all the interrupts
> before
> > + * setting up page table entries.
> > + */
> > +rtems_interrupt_disable(irq_level);
> > +arm_cp15_set_translation_table_entries(begin, end, access_flags);
> > +rtems_interrupt_enable(irq_level);
> > +
> > +return RTEMS_SUCCESSFUL;
> > +  }
> > +
> > +  return RTEMS_INVALID_ADDRESS;
> > +}
> > +
> > +rtems_status_code _Memory_protection_Unset_entries(uintptr_t begin,
> size_t size)
> > +{
> > +  uint32_t access_flags;
> > +  uintptr_t end;
> > +  rtems_interrupt_level irq_level;
> > +
> > +  if( validate_memory( begin, end ) == RTEMS_SUCCESSF