Part08 SystemStruct
Part08 SystemStruct
System Structure
Part One
This work is licensed under the Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License.
To view a copy of this license, visit http://creativecommons.org/licenses/bysa/2.0/uk/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California,
94105, USA.
System Structure
System Structure
System Structure
The phones
• Have a sophisticated graphical user interface (GUI) and a number of built-
in applications which use it
- For example, messaging and calendar
System Structure
System Structure
System Structure
EKA2
• Was first introduced in Symbian OS version 8.0b
• But first shipped in a phone product until version 8.1b
• Is found in the Japanese MOAP 2.0 FOMA 902i series
phones
• It is the second iteration of Symbian’s 32-bit kernel
• Very different internally to EKA1
• Offers hard real-time guarantees to kernel and user-mode
threads
DLLs in Symbian OS
A shared library
• Exports API functions according to a module definition
(.def) file
• It may have any number of exported functions
• Each is an entry point into the DLL
For example
• .fsy for a file system plug-in
• .prt for a protocol module plug-in
• File systems and Sockets are discussed later
ECOM Plug-ins
ECOM Plug-ins
UID1
UID1 Continued
UID2
UID3
EXEs
UID3
• on Symbian OS v9 and beyond UID3 should
usually be set to a unique value to act as the
secure identifier for the binary
• Pre-Symbian OS v9 it can be left unspecified
Functions to be exported
• Should be marked in the class definition in the header file with the
macro IMPORT_C
This is why:
• The IMPORT_C and EXPORT_C macros add functions to the export
table to make them accessible to components linking against the
library
• But the code of an inline function is already accessible to callers
because it is declared within the header file
• So the compiler interprets the inline directive by adding the code
directly into the client code wherever it calls it
• There is no need to export it
For example
• Code which links against a library and uses an exported function
with a specific ordinal number in an early version of the library
• Will not be able to call that function in a newer version of the library
if the ordinal number is changed
Note
On EKA1
• All GUI applications were built as DLLs
• No application code could use writable static or global data
On EKA2
• Applications are now built as EXEs, so this is no longer an issue
• Modifiable global or static data has always been allowed in EXEs
Symbian OS platform Writable static data in DLLs built for Application binary type
version
hardware
v6.1 — v8.0a Not supported on hardware builds DLL — no writable static data
(inclusive), v8.1a (compilation will fail) allowed
(EKA1)
v8.0b, v8.1b, v9.0 and Supported but not recommended — EXE — writable static data can be
beyond limited emulator support and used
(EKA2( inefficient in terms of memory usage
1. Thread-local storage
• One workaround used to replace writable static data is called thread-local
storage (TLS)
Thread-Local Storage
Functions Dll::SetTls() or
UserSvr::DllSetTls()
• Are used to save the pointer to the object
• To the thread-local storage pointer
2. Client–server framework
• Symbian OS supports writable global static data in EXEs
The memory for the object is pre-allocated in code but it does not
actually become initialized and constant
• Until after the constructor has run
Note
On EKA1
• The emulator can use the underlying Windows DLL mechanism to
provide per-process DLL data
• If non-constant global data is used inadvertently - it will go
undetected in emulator builds
• It will only fail when the PETRAN tool encounters it in the hardware
platform build
On target hardware
• Executable code can either be built onto the phone in read-only
memory (ROM) when the phone is in the factory
• Or can be later installed on the phone either into the phone’s
internal memory or onto removable storage media such as a
memory stick or MMC
ROM-based EXEs
• Can be thought of as executing directly in place from the ROM
• This means that program code and read-only data (such as literal
descriptors) are read directly from the ROM
• The component is only allocated a separate data area in RAM for
its read/write data.
DLLs in ROM
• Are not loaded into memory
• Execute in place in ROM at their fixed address
Symbian OS
• Copies it into the area of RAM reserved for program code
• Preparing it for execution by fixing up the relocation
information
Threads
Threads
• Are the basic unit of execution
• Form the basis of multitasking - allowing multiple sequences of
code to execute simultaneously (or appear to do so)
Threads
RHandleBase
RThread
Threads
Thread Creation
A thread is created
• In the suspended state
• Its execution started by a call to RThread::Resume()
Such as
• Its maximum and minimum size
• Whether it shares the creating thread’s heap or uses a specific
heap within the process in which it runs
Thread Heaps
Thread Identification
Alternatively
• The unique name of a thread can be passed to open a handle to
it
Thread Scheduling
Thread Termination
On EKA1
• A thread must call SetProtected()
• To prevent other process threads from acquiring a handle to it
• And killing it by making a call to Suspend(), Panic(),
Kill() or Terminate()
Thread Security
On EKA2 the security model ensures
• The thread is always protected from threads running in other
processes
• The redundant SetProtected() method has been removed
• A thread cannot stop another thread in a different process
The functions
• Suspend(), Terminate(), Kill() or Panic()
• Are still retained in EKA2
• A thread can still use these functions on itself
• Or other threads in the same process
• but not on threads in a different process
Thread Termination
If a secondary thread
• That is created by a call to RThread::Create()from with in the
process
• The thread terminates
• The process itself does not stop running
Thread Termination
TExitType RThread::ExitType()
• Allows the caller to distinguish between normal termination and a
panic
TInt RThread::ExitReason()
• Gets the specific reason associated with the end of this thread
TExitCategoryName RThread::ExitCategory()
• Gets the name of the category associated with the end of the
thread
Thread Notification
Semaphores
A semaphore
• Can be used either for sending a signal from one thread to
another
• Or for protecting a shared resource from being accessed by
multiple threads at the same time
A global semaphore
• Can be created, opened and used by any process in the system
A local semaphore
• Can be restricted to all threads within a single process
Semaphore
Mutexes
A mutex
• Is used to protect a shared resource
• So that it can only be accessed by one thread at a time
• The RMutex class is used to create and access global and local
mutexes
Critical Sections
A critical section
• Is a region of code that should not be entered simultaneously by
multiple threads
RCriticalSection objects
• Are always local to a process
Processes
Note
Processes
A Symbian OS process
• Is an executable that has its own data area, stack and heap
• By default a process is given 8 KB of stack and 1 MB of heap
• Sometimes referred to as a unit of protection
Processes
By default
• A process contains a single execution thread - the main
thread
• Additional threads can be created as described above
Processes
Processes
The class used to manipulate processes is
RProcess
RHandleBase
RProcess
Processes
Processes
On Windows
• The emulator runs within a single Win32 process called EPOC.exe
• Each Symbian OS process runs as a separate thread inside it
On EKA1
• The emulation of processes on Windows is incomplete
• RProcess::Create() returns KErrNotFound
On EKA2
• This has been removed
• Symbian OS still runs in a single process
• But the emulation is enhanced ...
• RProcess::Create() translates to creation of a new Win32
thread
DLLs in Symbian OS
System Structure
Part Two
System Structure
Client-Server
Session-based communication
• Ensures that all clients will be notified in the case of an error or shutdown of
a server
• All server resources will be cleaned up if an error occur
• Or when a client disconnects or dies
Client-Server
Do not worry about the details now as client-server architecture
shall be examined in a later lecture
user-side kernel-side
client server
mediated by the kernel
Session based clients can serializes requests
have more than one session
CSession2 DSession
CServer2
RServer2 DServer
Client-Server
It is used
• To allow a device driver and its clients to access the same
memory area
• Without copying even during interrupt handling
Properties
Set
Subscriber
Get
P1
Publisher
Get
Publisher/
P2
Subscriber
Set
Get
Publisher P3
Subscriber
Subscribers
• Do not need to know which component is publishing to a property
RHandleBase
RProperty
On EKA2
• The benefit of attaching to an existing handle is that it has a
deterministic bounded execution time
• This makes it suitable for high-priority real-time tasks
A property is published
• By calling RProperty::Set()
• This writes a new value “atomically” to the property
• Ensuring that access by multiple threads is handled correctly
To subscribe to a property
• A client must register interest by attaching to it
• Calling the asynchronous RProperty::Subscribe()
method
A typical example
• Is the notification of a change to the device’s radio states
For example
• Flight-mode
• Bluetooth radio on/off
• WiFi on/off
Alternatively
• The process calling RProperty::Define() must have
WriteDeviceData capability
For example
• Before accepting a subscription to a property
• The security policy defined when the property was created is
checked
• The subscription request completes with
KErrPermissionDenied if the check fails
Message Queues
Message queues
• Provide a way to send data (messages) to interested parties
• Without needing to know whether any thread is listening
• Or the identity of a recipient
Message Queues
user-side kernel-side
Msg Q Handle
DMsgQueue
Msg Q Handle
The queue
Msg Q Handle 1
Msg Q Handle 2
Message Queues
A message
• Is an object that is placed into a queue for delivery to
recipients
• A queue is normally created for messages of a given type
A queue
• Is created to deal with messages of a defined (fixed) length
• Which must be a multiple of four bytes
Message Queues
Message Queues
A message queue
• Allows two or more threads to communicate without setting up a
connection to each other
Within a process
• The messages can point to memory mapped to that process and
can be used for passing descriptors and pointers between threads
Message Queues
Message queues
• Are useful for allowing information to be communicated beyond the
lifetime of the sender
Message Queues
However
• Neither messages nor queues are persistent
• They are cleaned up when the last handle to the queue is closed
Recognizers
Recognizers
Recognizers
• Are a good example of the use of framework plug-in DLLs
• The framework which loads the recognizers is provided by the
application architecture server (Apparc)
Up to Symbian OS v9.1
• Apparc implemented its own custom loading of recognizer plug-ins
Recognizers
Recognizers
DoRecognizeL()
DoRecognizeL()
• Performs data recognition
• This function is not pure virtual but must be implemented
Each implementation
• Should set a value to indicate the MIME type it considers the data to
belong to
SupportedDataTypeL()
SupportedDataTypeL()
• Returns the MIME types that the recognizer is capable of
recognizing
• This pure virtual function must be implemented by all recognizer
plug-ins
PreferredBufSize()
PreferredBufSize()
• Specifies the size in bytes of the buffer passed to
DoRecognizeL()
• That the recognizer needs to work with
• This function is not pure virtual but must be implemented
Panics
Panics
Panics
On phone hardware
• And in release builds on the Windows emulator
• The end result of a panic is either a reboot or an “Application
closed” message box
Panics
On EKA2
• A thread may panic any other thread in the same process
• By acquiring an RThread handle and using it to call
RThread::Panic()
On EKA1
• This function could be used to panic any unprotected thread in any
process
• This was deemed insecure for EKA2
Panics
Panics
Panics
Panics
User::Panic(KFooDllBarAPI, KErrArgument);
Panics
Symbian OS
• Has a series of well-documented panic categories for
example:
• KERN-EXEC
• E32USER-CBASE
• ALLOC
• USER
• And associated error values
Assertions
Typically
• An assertion evaluates a statement
• If it is false it halts execution of the code
Assertions
Assertions
Assertions
Assertions
Assertions
It is more common for a class or code module to define:
• A panic function
• A panic category string
• A set of specific panic enumerators
For example
• The following enumeration could be added to CTestClass
• So as not to pollute the global namespace
enum TTestClassPanic
{
EEatPiesInvalidArgument, // Invalid argument passed to EatPies()
... // Enum values for assertions
// in other CTestClass methods
};
Assertions
A panic function is defined either
• As a member of the class
• Or as a static function within the file containing the implementation
of the class:
Assertions
Assertions
Assertions
Assertions
__ASSERT_DEBUG(FunctionReturningTrue(), Panic(EUnexpectedReturnValue));
__ASSERT_DEBUG(++index<=KMaxValue, Panic(EInvalidIndex));
Leaves
• May legitimately occur under exceptional
conditions
Such as:
• Out of memory
• Insufficient disk space
• Or the absence of a communications link
It is not possible
• To stop a leave from occurring
• Code should implement a graceful recovery
strategy
• Always catch leaves using TRAP statements
Recognizers