1 | /* -*- c++ -*- |
---|
2 | * |
---|
3 | * SOCLIB_LGPL_HEADER_BEGIN |
---|
4 | * |
---|
5 | * This file is part of SoCLib, GNU LGPLv2.1. |
---|
6 | * |
---|
7 | * SoCLib is free software; you can redistribute it and/or modify it |
---|
8 | * under the terms of the GNU Lesser General Public License as published |
---|
9 | * by the Free Software Foundation; version 2.1 of the License. |
---|
10 | * |
---|
11 | * SoCLib is distributed in the hope that it will be useful, but |
---|
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | * Lesser General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU Lesser General Public |
---|
17 | * License along with SoCLib; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
19 | * 02110-1301 USA |
---|
20 | * |
---|
21 | * SOCLIB_LGPL_HEADER_END |
---|
22 | * |
---|
23 | * Copyright (c) UPMC, Lip6, SoC |
---|
24 | * Mohamed Lamine Karaoui <Mohamed.Karaoui@lip6.fr>, 2012 |
---|
25 | */ |
---|
26 | |
---|
27 | #include <algorithm> |
---|
28 | #include <string.h> |
---|
29 | #include <cassert> |
---|
30 | #include <cstring> |
---|
31 | #include <stdexcept> |
---|
32 | |
---|
33 | #include <iostream> |
---|
34 | #include <sstream> |
---|
35 | #include <iomanip> |
---|
36 | |
---|
37 | #include "pseg.h" |
---|
38 | #include "exception.h" |
---|
39 | |
---|
40 | |
---|
41 | /* |
---|
42 | * VSeg |
---|
43 | */ |
---|
44 | |
---|
45 | const std::string & VSeg::name() const |
---|
46 | { |
---|
47 | return m_name; |
---|
48 | } |
---|
49 | |
---|
50 | const std::string & VSeg::file() const |
---|
51 | { |
---|
52 | return m_file; |
---|
53 | } |
---|
54 | |
---|
55 | uintptr_t VSeg::vma() const |
---|
56 | { |
---|
57 | return m_vma; |
---|
58 | } |
---|
59 | |
---|
60 | uintptr_t VSeg::lma() const |
---|
61 | { |
---|
62 | return m_lma; |
---|
63 | } |
---|
64 | |
---|
65 | uintptr_t VSeg::length() const |
---|
66 | { |
---|
67 | return m_length; |
---|
68 | } |
---|
69 | |
---|
70 | void VSeg::print( std::ostream &o ) const |
---|
71 | { |
---|
72 | o << std::hex << std::noshowbase |
---|
73 | << "<Virtual segment from(vaddr): 0x" |
---|
74 | << std::setw (8) << std::setfill('0') |
---|
75 | << m_vma << ", to(paddr) 0x" |
---|
76 | << std::setw (8) << std::setfill('0') |
---|
77 | << m_lma << ", size: 0x" |
---|
78 | << std::setw (8) << std::setfill('0') |
---|
79 | << m_length << ",ident: " |
---|
80 | << (m_ident ? "yes" : "no") << ", in(file): " |
---|
81 | << m_file << ", name: " << m_name << ">"; |
---|
82 | } |
---|
83 | |
---|
84 | VSeg::~VSeg() |
---|
85 | { |
---|
86 | // std::cout << "Deleted VSeg " << *this << std::endl; |
---|
87 | } |
---|
88 | |
---|
89 | VSeg & VSeg::operator=( const VSeg &ref ) |
---|
90 | { |
---|
91 | if ( &ref == this ) |
---|
92 | return *this; |
---|
93 | |
---|
94 | //std::cout << "Copying " << ref << " to " << *this << std::endl; |
---|
95 | m_name = ref.m_name, |
---|
96 | m_file = ref.m_file; |
---|
97 | m_vma = ref.m_vma; |
---|
98 | m_lma = ref.m_lma; |
---|
99 | m_length = ref.m_length; |
---|
100 | m_ident = ref.m_ident; |
---|
101 | return *this; |
---|
102 | } |
---|
103 | |
---|
104 | VSeg::VSeg() |
---|
105 | : m_name("No Name"), |
---|
106 | m_file("Empty section"), |
---|
107 | m_vma(0), |
---|
108 | m_length(0), |
---|
109 | m_loadable(false), |
---|
110 | m_ident(0) |
---|
111 | { |
---|
112 | //std::cout << "New empty VSeg " << *this << std::endl; |
---|
113 | } |
---|
114 | |
---|
115 | VSeg::VSeg(std::string& binaryName, std::string& name, uintptr_t vma, size_t length, bool loadable, bool ident) |
---|
116 | : m_name(name), |
---|
117 | m_file(binaryName), |
---|
118 | m_vma(vma), |
---|
119 | m_length(length), |
---|
120 | m_loadable(loadable), |
---|
121 | m_ident(ident) |
---|
122 | { |
---|
123 | //std::cout << "New VSeg " << *this << std::endl; |
---|
124 | } |
---|
125 | |
---|
126 | VSeg::VSeg( const VSeg &ref ) |
---|
127 | : m_name("To be copied"), |
---|
128 | m_file("Empty"), |
---|
129 | m_vma(0), |
---|
130 | m_length(0), |
---|
131 | m_loadable(false), |
---|
132 | m_ident(0) |
---|
133 | { |
---|
134 | //std::cout << "New VSeg " << *this << " copied from " << ref << std::endl; |
---|
135 | (*this) = ref; |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | /* |
---|
142 | * PSeg |
---|
143 | */ |
---|
144 | uintptr_t PSeg::lma() const |
---|
145 | { |
---|
146 | return m_lma; |
---|
147 | } |
---|
148 | |
---|
149 | uintptr_t PSeg::limit() const |
---|
150 | { |
---|
151 | return m_limit; |
---|
152 | } |
---|
153 | |
---|
154 | uintptr_t PSeg::length() const |
---|
155 | { |
---|
156 | return m_length; |
---|
157 | } |
---|
158 | |
---|
159 | uintptr_t PSeg::nextLma() const |
---|
160 | { |
---|
161 | return m_nextLma; |
---|
162 | } |
---|
163 | |
---|
164 | const std::string & PSeg::name() const |
---|
165 | { |
---|
166 | return m_name; |
---|
167 | } |
---|
168 | |
---|
169 | void PSeg::check() const |
---|
170 | { |
---|
171 | size_t size = m_vsegs.size(); |
---|
172 | size_t used[size][2];//lma, lma+length |
---|
173 | size_t i,j,error=0; |
---|
174 | |
---|
175 | std::vector<VSeg>::const_iterator it; |
---|
176 | for(it = m_vsegs.begin(), i= 0; it < m_vsegs.end(); it++, i++) |
---|
177 | { |
---|
178 | size_t it_limit = (*it).lma() + (*it).length(); |
---|
179 | for(j=0; j< i; j++) |
---|
180 | { |
---|
181 | if( |
---|
182 | (used[j][0] == (*it).lma() /*and (*it).length()?*/) ) //not the same lma , |
---|
183 | { |
---|
184 | error = 1; |
---|
185 | std::cout << "ok \n"; |
---|
186 | } |
---|
187 | if( |
---|
188 | (used[j][1] == it_limit /*and (*it).legth()?*/)) // and not the same limit |
---|
189 | { |
---|
190 | error = 2; |
---|
191 | } |
---|
192 | if( |
---|
193 | ((used[j][0] < (*it).lma()) and ((*it).lma() < used[j][1] )) ) //lma within the used slice |
---|
194 | { |
---|
195 | error = 3; |
---|
196 | } |
---|
197 | if( |
---|
198 | ((used[j][0] < it_limit) and (it_limit < used[j][1] )) )//limit not within the used slice |
---|
199 | { |
---|
200 | error = 4; |
---|
201 | std::cout << "limit: " << std::hex << it_limit << std::endl; |
---|
202 | std::cout << "used[j][0]: " << std::hex << used[j][0] << std::endl; |
---|
203 | std::cout << "used[j][1]: " << std::hex << used[j][1] << std::endl; |
---|
204 | } |
---|
205 | if(error) |
---|
206 | { |
---|
207 | std::ostringstream err; |
---|
208 | err << " Error" << error << " ,ovelapping Buffers:" << std::endl |
---|
209 | << *it << std::endl << m_vsegs[j] << std::endl; |
---|
210 | throw exception::RunTimeError( err.str().c_str() ); |
---|
211 | } |
---|
212 | |
---|
213 | } |
---|
214 | used[i][0] = (*it).lma(); |
---|
215 | used[i][1] = it_limit; |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | void PSeg::setName(std::string& name ) |
---|
220 | { |
---|
221 | m_name = name; |
---|
222 | } |
---|
223 | |
---|
224 | size_t PSeg::align( unsigned toAlign, unsigned alignPow2) |
---|
225 | { |
---|
226 | return ((toAlign + (1 << alignPow2) - 1 ) >> alignPow2) << alignPow2;//page aligned |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | size_t PSeg::pageAlign( size_t toAlign ) |
---|
231 | { |
---|
232 | size_t pgs = pageSize(); |
---|
233 | size_t pageSizePow2 = __builtin_ctz(pgs); |
---|
234 | |
---|
235 | return align(toAlign, pageSizePow2);//page aligned |
---|
236 | |
---|
237 | } |
---|
238 | |
---|
239 | void PSeg::setLma( uintptr_t lma ) |
---|
240 | { |
---|
241 | m_lma = lma; |
---|
242 | |
---|
243 | m_nextLma = pageAlign(lma);//page aligned |
---|
244 | |
---|
245 | m_pageLimit = pageAlign(m_lma+m_length); |
---|
246 | |
---|
247 | m_limit = (m_lma + m_length); |
---|
248 | |
---|
249 | } |
---|
250 | |
---|
251 | void PSeg::setLength( size_t length ) |
---|
252 | { |
---|
253 | m_length = length; |
---|
254 | |
---|
255 | m_pageLimit = pageAlign(m_lma+m_length); |
---|
256 | |
---|
257 | m_limit = (m_lma + m_length); |
---|
258 | |
---|
259 | //std::cout << std::hex << " length seted, m_limit: " << m_limit << std::endl; |
---|
260 | //std::cout << *this <<std::endl; |
---|
261 | } |
---|
262 | |
---|
263 | void PSeg::add( VSeg& vseg ) |
---|
264 | { |
---|
265 | vseg.m_lma = m_nextLma; |
---|
266 | incNextLma(vseg.length());//for the next vseg |
---|
267 | m_vsegs.push_back(vseg); |
---|
268 | } |
---|
269 | |
---|
270 | void PSeg::addIdent( VSeg& vseg ) |
---|
271 | { |
---|
272 | vseg.m_lma = vseg.m_vma; |
---|
273 | incNextLma(vseg.length());//to keep track of space used |
---|
274 | m_vsegs.push_back(vseg); |
---|
275 | } |
---|
276 | |
---|
277 | void PSeg::setNextLma( uintptr_t nextLma) |
---|
278 | { |
---|
279 | m_nextLma = nextLma; |
---|
280 | |
---|
281 | confNextLma(); |
---|
282 | } |
---|
283 | |
---|
284 | void PSeg::incNextLma( size_t inc_next) |
---|
285 | { |
---|
286 | |
---|
287 | m_nextLma += inc_next; |
---|
288 | |
---|
289 | confNextLma(); |
---|
290 | } |
---|
291 | |
---|
292 | void PSeg::confNextLma() |
---|
293 | { |
---|
294 | if(m_nextLma > m_limit) |
---|
295 | { |
---|
296 | std::cerr << "Erreur pseg overflow... nextLma: " |
---|
297 | << std::hex << m_nextLma << ", limit: " |
---|
298 | << m_limit << std::endl; |
---|
299 | exit(1); |
---|
300 | } |
---|
301 | |
---|
302 | m_nextLma = pageAlign( m_nextLma ); |
---|
303 | |
---|
304 | if(m_nextLma > m_pageLimit) |
---|
305 | { |
---|
306 | std::cerr << "Erreur pseg page overflow... nextLma: " |
---|
307 | << std::hex << m_nextLma << ", limit: " |
---|
308 | << m_pageLimit << std::endl; |
---|
309 | exit(1); |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | void PSeg::setPageSize(size_t pg) |
---|
314 | { |
---|
315 | if( pg == 0) |
---|
316 | { |
---|
317 | std::cerr << "PageSize must be positive" << std::endl; |
---|
318 | return; |
---|
319 | } |
---|
320 | pageSize() = pg; |
---|
321 | } |
---|
322 | |
---|
323 | size_t& PSeg::pageSize() |
---|
324 | { |
---|
325 | static size_t m_pageSize; |
---|
326 | return m_pageSize; |
---|
327 | } |
---|
328 | |
---|
329 | PSeg & PSeg::operator=( const PSeg &ref ) |
---|
330 | { |
---|
331 | if ( &ref == this ) |
---|
332 | return *this; |
---|
333 | |
---|
334 | //std::cout << "Copying " << ref << " to " << *this << std::endl; |
---|
335 | |
---|
336 | m_name = ref.m_name; |
---|
337 | m_length = ref.m_length; |
---|
338 | m_limit = ref.m_limit; |
---|
339 | m_pageLimit = ref.m_pageLimit; |
---|
340 | m_lma = ref.m_lma; |
---|
341 | m_nextLma = ref.m_nextLma; |
---|
342 | m_vsegs = ref.m_vsegs; |
---|
343 | |
---|
344 | return *this; |
---|
345 | } |
---|
346 | |
---|
347 | void PSeg::print( std::ostream &o ) const |
---|
348 | { |
---|
349 | o << "<Physical segment " |
---|
350 | << std::showbase << m_name |
---|
351 | << ", from: " << std::hex |
---|
352 | << m_lma << " to " << m_limit |
---|
353 | << ", size : " << m_length |
---|
354 | << ", filled to: " << m_nextLma |
---|
355 | << ", containing: "<< std::endl; |
---|
356 | std::vector<VSeg>::const_iterator it; |
---|
357 | for(it = m_vsegs.begin(); it < m_vsegs.end(); it++) |
---|
358 | o << " " << *it << std::endl; |
---|
359 | |
---|
360 | o << ">"; |
---|
361 | } |
---|
362 | |
---|
363 | PSeg::PSeg( const std::string &name, |
---|
364 | uintptr_t lma, |
---|
365 | size_t length) |
---|
366 | { |
---|
367 | m_name = name; |
---|
368 | m_length = length; |
---|
369 | |
---|
370 | setLma(lma); |
---|
371 | //std::cout <<"New PSeg :"<< *this ; |
---|
372 | } |
---|
373 | |
---|
374 | PSeg::PSeg( const std::string &name): |
---|
375 | m_lma(0), |
---|
376 | m_length(0), |
---|
377 | m_nextLma(0), |
---|
378 | m_limit(0) |
---|
379 | { |
---|
380 | m_name = name; |
---|
381 | } |
---|
382 | |
---|
383 | PSeg::PSeg( uintptr_t lma): |
---|
384 | m_name("No name"), |
---|
385 | m_lma(0), |
---|
386 | m_length(0), |
---|
387 | m_nextLma(0), |
---|
388 | m_limit(0) |
---|
389 | { |
---|
390 | setLma(lma); |
---|
391 | } |
---|
392 | |
---|
393 | |
---|
394 | PSeg::PSeg() |
---|
395 | : |
---|
396 | m_name("Empty section"), |
---|
397 | m_lma(0), |
---|
398 | m_length(0), |
---|
399 | m_nextLma(0), |
---|
400 | m_limit(0) |
---|
401 | { |
---|
402 | //std::cout << "New empty PSeg " << *this << std::endl; |
---|
403 | } |
---|
404 | |
---|
405 | PSeg::PSeg( const PSeg &ref ) |
---|
406 | : m_name("To be copied"), |
---|
407 | m_lma(0), |
---|
408 | m_length(0), |
---|
409 | m_nextLma(0), |
---|
410 | m_limit(0) |
---|
411 | { |
---|
412 | //std::cout << "New PSeg " << *this << " copied from " << ref << std::endl; |
---|
413 | (*this) = ref; |
---|
414 | } |
---|
415 | |
---|
416 | PSeg::~PSeg() |
---|
417 | { |
---|
418 | // std::cout << "Deleted PSeg " << *this << std::endl; |
---|
419 | } |
---|
420 | |
---|
421 | |
---|
422 | |
---|
423 | // Local Variables: |
---|
424 | // tab-width: 4 |
---|
425 | // c-basic-offset: 4 |
---|
426 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
427 | // indent-tabs-mode: nil |
---|
428 | // End: |
---|
429 | |
---|
430 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
431 | |
---|