Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:22

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSImplClassicPartition
0007  *
0008  * @brief This source file contains the implementation of
0009  *   rtems_partition_create() and the Partition Manager system initialization.
0010  */
0011 
0012 /*
0013  *  COPYRIGHT (c) 1989-2014.
0014  *  On-Line Applications Research Corporation (OAR).
0015  *
0016  * Redistribution and use in source and binary forms, with or without
0017  * modification, are permitted provided that the following conditions
0018  * are met:
0019  * 1. Redistributions of source code must retain the above copyright
0020  *    notice, this list of conditions and the following disclaimer.
0021  * 2. Redistributions in binary form must reproduce the above copyright
0022  *    notice, this list of conditions and the following disclaimer in the
0023  *    documentation and/or other materials provided with the distribution.
0024  *
0025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0028  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0029  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0030  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0031  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0034  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0035  * POSSIBILITY OF SUCH DAMAGE.
0036  */
0037 
0038 #ifdef HAVE_CONFIG_H
0039 #include "config.h"
0040 #endif
0041 
0042 #include <rtems/rtems/partimpl.h>
0043 #include <rtems/rtems/attrimpl.h>
0044 #include <rtems/rtems/support.h>
0045 #include <rtems/score/address.h>
0046 #include <rtems/score/chainimpl.h>
0047 #include <rtems/score/sysstate.h>
0048 #include <rtems/sysinit.h>
0049 
0050 static Partition_Control *_Partition_Allocate( void )
0051 {
0052   return (Partition_Control *) _Objects_Allocate( &_Partition_Information );
0053 }
0054 
0055 static void _Partition_Initialize(
0056   Partition_Control *the_partition,
0057   void              *starting_address,
0058   uintptr_t          length,
0059   size_t             buffer_size,
0060   rtems_attribute    attribute_set
0061 )
0062 {
0063   const void *limit_address;
0064 
0065   limit_address = _Addresses_Add_offset( starting_address, length - 1 );
0066   the_partition->base_address          = starting_address;
0067   the_partition->limit_address         = limit_address;
0068   the_partition->buffer_size           = buffer_size;
0069   the_partition->attribute_set         = attribute_set;
0070   the_partition->number_of_used_blocks = 0;
0071 
0072   _Chain_Initialize(
0073     &the_partition->Memory,
0074     starting_address,
0075     length / buffer_size,
0076     buffer_size
0077   );
0078 
0079   _ISR_lock_Initialize( &the_partition->Lock, "Partition" );
0080 }
0081 
0082 rtems_status_code rtems_partition_create(
0083   rtems_name       name,
0084   void            *starting_address,
0085   uintptr_t        length,
0086   size_t           buffer_size,
0087   rtems_attribute  attribute_set,
0088   rtems_id        *id
0089 )
0090 {
0091   Partition_Control *the_partition;
0092 
0093   if ( !rtems_is_name_valid( name ) ) {
0094     return RTEMS_INVALID_NAME;
0095   }
0096 
0097   if ( id == NULL ) {
0098     return RTEMS_INVALID_ADDRESS;
0099   }
0100 
0101   if ( starting_address == NULL ) {
0102     return RTEMS_INVALID_ADDRESS;
0103   }
0104 
0105   if ( length == 0 ) {
0106     return RTEMS_INVALID_SIZE;
0107   }
0108 
0109   if ( buffer_size == 0 ) {
0110     return RTEMS_INVALID_SIZE;
0111   }
0112 
0113   if ( length < buffer_size ) {
0114     return RTEMS_INVALID_SIZE;
0115   }
0116 
0117   /*
0118    * Ensure that the buffer size is an integral multiple of the pointer size so
0119    * that each buffer begin meets the chain node alignment.
0120    */
0121   if ( buffer_size % CPU_SIZEOF_POINTER != 0 ) {
0122     return RTEMS_INVALID_SIZE;
0123   }
0124 
0125   if ( buffer_size < sizeof( Chain_Node ) )
0126     return RTEMS_INVALID_SIZE;
0127 
0128   /*
0129    * Ensure that the buffer area starting address is aligned on a pointer
0130    * boundary so that each buffer begin meets the chain node alignment.
0131    */
0132   if ( (uintptr_t) starting_address % CPU_SIZEOF_POINTER != 0 ) {
0133     return RTEMS_INVALID_ADDRESS;
0134   }
0135 
0136 #if defined(RTEMS_MULTIPROCESSING)
0137   if ( !_System_state_Is_multiprocessing ) {
0138     attribute_set = _Attributes_Clear( attribute_set, RTEMS_GLOBAL );
0139   }
0140 #endif
0141 
0142   the_partition = _Partition_Allocate();
0143 
0144   if ( !the_partition ) {
0145     _Objects_Allocator_unlock();
0146     return RTEMS_TOO_MANY;
0147   }
0148 
0149 #if defined(RTEMS_MULTIPROCESSING)
0150   if ( _Attributes_Is_global( attribute_set ) &&
0151        !( _Objects_MP_Allocate_and_open( &_Partition_Information, name,
0152                             the_partition->Object.id, false ) ) ) {
0153     _Objects_Free( &_Partition_Information, &the_partition->Object );
0154     _Objects_Allocator_unlock();
0155     return RTEMS_TOO_MANY;
0156   }
0157 #endif
0158 
0159   _Partition_Initialize(
0160     the_partition,
0161     starting_address,
0162     length,
0163     buffer_size,
0164     attribute_set
0165   );
0166 
0167   *id = _Objects_Open_u32(
0168     &_Partition_Information,
0169     &the_partition->Object,
0170     name
0171   );
0172 
0173 #if defined(RTEMS_MULTIPROCESSING)
0174   if ( _Attributes_Is_global( attribute_set ) )
0175     _Partition_MP_Send_process_packet(
0176       PARTITION_MP_ANNOUNCE_CREATE,
0177       the_partition->Object.id,
0178       name,
0179       0                  /* Not used */
0180     );
0181 #endif
0182 
0183   _Objects_Allocator_unlock();
0184   return RTEMS_SUCCESSFUL;
0185 }
0186 
0187 static void _Partition_Manager_initialization( void )
0188 {
0189   _Objects_Initialize_information( &_Partition_Information );
0190 }
0191 
0192 RTEMS_SYSINIT_ITEM(
0193   _Partition_Manager_initialization,
0194   RTEMS_SYSINIT_CLASSIC_PARTITION,
0195   RTEMS_SYSINIT_ORDER_MIDDLE
0196 );