• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** @addtogroup MCD_IMPL_LIB
2  * @{
3  * @file
4  * <!-- Copyright Giesecke & Devrient GmbH 2009 - 2012 -->
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote
15  *    products derived from this software without specific prior
16  *    written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #ifndef SESSION_H_
31 #define SESSION_H_
32 
33 #include <stdint.h>
34 #include <list>
35 
36 #include "mc_linux.h"
37 #include "Connection.h"
38 #include "CMcKMod.h"
39 #include "CMutex.h"
40 
41 
42 class BulkBufferDescriptor
43 {
44 public:
45     addr_t    virtAddr; /**< The virtual address of the Bulk buffer*/
46     addr_t    sVirtualAddr; /**< The secure virtual address of the Bulk buffer*/
47     uint32_t  len; /**< Length of the Bulk buffer*/
48     uint32_t  handle;
49     addr_t    physAddrWsmL2; /**< The physical address of the L2 table of the Bulk buffer*/
50 
BulkBufferDescriptor(addr_t virtAddr,addr_t sVirtAddr,uint32_t len,uint32_t handle,addr_t physAddrWsmL2)51     BulkBufferDescriptor(
52         addr_t    virtAddr,
53         addr_t    sVirtAddr,
54         uint32_t  len,
55         uint32_t  handle,
56         addr_t    physAddrWsmL2
57     ) :
58         virtAddr(virtAddr),
59         sVirtualAddr(sVirtAddr),
60         len(len),
61         handle(handle),
62         physAddrWsmL2(physAddrWsmL2)
63     {};
64 
65 };
66 
67 typedef std::list<BulkBufferDescriptor *>  bulkBufferDescrList_t;
68 typedef bulkBufferDescrList_t::iterator   bulkBufferDescrIterator_t;
69 
70 
71 /** Session states.
72  * At the moment not used !!.
73  */
74 typedef enum {
75     SESSION_STATE_INITIAL,
76     SESSION_STATE_OPEN,
77     SESSION_STATE_TRUSTLET_DEAD
78 } sessionState_t;
79 
80 #define SESSION_ERR_NO      0 /**< No session error */
81 
82 /** Session information structure.
83  * The information structure is used to hold the state of the session, which will limit further actions for the session.
84  * Also the last error code will be stored till it's read.
85  */
86 typedef struct {
87     sessionState_t state;       /**< Session state */
88     int32_t        lastErr;     /**< Last error of session */
89 } sessionInformation_t;
90 
91 
92 class Session
93 {
94 private:
95     CMcKMod *mcKMod;
96     CMutex workLock;
97     bulkBufferDescrList_t bulkBufferDescriptors; /**< Descriptors of additional bulk buffer of a session */
98     sessionInformation_t sessionInfo; /**< Informations about session */
99 public:
100     uint32_t sessionId;
101     Connection *notificationConnection;
102 
103     Session(uint32_t sessionId, CMcKMod *mcKMod, Connection *connection);
104 
105     virtual ~Session(void);
106 
107     /**
108      * Add address information of additional bulk buffer memory to session and
109      * register virtual memory in kernel module.
110      *
111      * @attention The virtual address can only be added one time. If the virtual address already exist, MC_DRV_ERR_BUFFER_ALREADY_MAPPED is returned.
112      *
113      * @param buf The virtual address of bulk buffer.
114      * @param len Length of bulk buffer.
115      * @param blkBuf pointer of the actual Bulk buffer descriptor with all address information.
116      *
117      * @return MC_DRV_OK on success
118      * @return MC_DRV_ERR_BUFFER_ALREADY_MAPPED
119      */
120     mcResult_t addBulkBuf(addr_t buf, uint32_t len, BulkBufferDescriptor **blkBuf);
121 
122     /**
123      * Remove address information of additional bulk buffer memory from session and
124      * unregister virtual memory in kernel module
125      *
126      * @param buf The virtual address of the bulk buffer.
127      *
128      * @return true on success.
129      */
130     mcResult_t removeBulkBuf(addr_t buf);
131 
132     /**
133      * Return the Kmod handle of the bulk buff
134      *
135      * @param buf The secure virtual address of the bulk buffer.
136      *
137      * @return the Handle or 0 for failure
138      */
139     uint32_t getBufHandle(addr_t sVirtualAddr);
140 
141     /**
142      * Set additional error information of the last error that occured.
143      *
144      * @param errorCode The actual error.
145      */
146     void setErrorInfo(int32_t err);
147 
148     /**
149      * Get additional error information of the last error that occured.
150      *
151      * @attention After request the information is set to SESSION_ERR_NO.
152      *
153      * @return Last stored error code or SESSION_ERR_NO.
154      */
155     int32_t getLastErr(void);
156 
157     /**
158      * Lock session for operation
159      */
lock()160     void lock() {
161         workLock.lock();
162     }
163 
164     /**
165      * Unlock session for operation
166      */
unlock()167     void unlock()  {
168         workLock.unlock();
169     }
170 };
171 
172 typedef std::list<Session *>            sessionList_t;
173 typedef sessionList_t::iterator        sessionIterator_t;
174 
175 #endif /* SESSION_H_ */
176 
177 /** @} */
178