Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(976)

Side by Side Diff: src/platform-openbsd.cc

Issue 465002: Initial OpenBSD support... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/dtoa-config.c ('k') | src/platform-posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ native
OLDNEW
(Empty)
1 // Copyright 2006-2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 // Platform specific code for OpenBSD goes here. For the POSIX comaptible parts
29 // the implementation is in platform-posix.cc.
30
31 #include <pthread.h>
32 #include <semaphore.h>
33 #include <signal.h>
34 #include <sys/time.h>
35 #include <sys/resource.h>
36 #include <sys/types.h>
37 #include <stdlib.h>
38
39 #include <sys/types.h> // mmap & munmap
40 #include <sys/mman.h> // mmap & munmap
41 #include <sys/stat.h> // open
42 #include <sys/fcntl.h> // open
43 #include <unistd.h> // getpagesize
44 #include <execinfo.h> // backtrace, backtrace_symbols
45 #include <strings.h> // index
46 #include <errno.h>
47 #include <stdarg.h>
48 #include <limits.h>
49
50 #undef MAP_TYPE
51
52 #include "v8.h"
53
54 #include "platform.h"
55
56
57 namespace v8 {
58 namespace internal {
59
60 // 0 is never a valid thread id on OpenBSD since tids and pids share a
61 // name space and pid 0 is used to kill the group (see man 2 kill).
62 static const pthread_t kNoThread = (pthread_t) 0;
63
64
65 double ceiling(double x) {
66 // Correct as on OS X
67 if (-1.0 < x && x < 0.0) {
68 return -0.0;
69 } else {
70 return ceil(x);
71 }
72 }
73
74
75 void OS::Setup() {
76 // Seed the random number generator.
77 // Convert the current time to a 64-bit integer first, before converting it
78 // to an unsigned. Going directly can cause an overflow and the seed to be
79 // set to all ones. The seed will be identical for different instances that
80 // call this setup code within the same millisecond.
81 uint64_t seed = static_cast<uint64_t>(TimeCurrentMillis());
82 srandom(static_cast<unsigned int>(seed));
83 }
84
85
86 double OS::nan_value() {
87 return NAN;
88 }
89
90
91 int OS::ActivationFrameAlignment() {
92 // 16 byte alignment on OpenBSD
93 return 16;
94 }
95
96
97 // We keep the lowest and highest addresses mapped as a quick way of
98 // determining that pointers are outside the heap (used mostly in assertions
99 // and verification). The estimate is conservative, ie, not all addresses in
100 // 'allocated' space are actually allocated to our heap. The range is
101 // [lowest, highest), inclusive on the low and and exclusive on the high end.
102 static void* lowest_ever_allocated = reinterpret_cast<void*>(-1);
103 static void* highest_ever_allocated = reinterpret_cast<void*>(0);
104
105
106 static void UpdateAllocatedSpaceLimits(void* address, int size) {
107 lowest_ever_allocated = Min(lowest_ever_allocated, address);
108 highest_ever_allocated =
109 Max(highest_ever_allocated,
110 reinterpret_cast<void*>(reinterpret_cast<char*>(address) + size));
111 }
112
113
114 bool OS::IsOutsideAllocatedSpace(void* address) {
115 return address < lowest_ever_allocated || address >= highest_ever_allocated;
116 }
117
118
119 size_t OS::AllocateAlignment() {
120 return getpagesize();
121 }
122
123
124 void* OS::Allocate(const size_t requested,
125 size_t* allocated,
126 bool executable) {
127 const size_t msize = RoundUp(requested, getpagesize());
128 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0);
129 void* mbase = mmap(NULL, msize, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
130
131 if (mbase == MAP_FAILED) {
132 LOG(StringEvent("OS::Allocate", "mmap failed"));
133 return NULL;
134 }
135 *allocated = msize;
136 UpdateAllocatedSpaceLimits(mbase, msize);
137 return mbase;
138 }
139
140
141 void OS::Free(void* buf, const size_t length) {
142 int result = munmap(buf, length);
143 USE(result);
144 ASSERT(result == 0);
145 }
146
147
148 #ifdef ENABLE_HEAP_PROTECTION
149
150 void OS::Protect(void* address, size_t size) {
151 UNIMPLEMENTED();
152 }
153
154
155 void OS::Unprotect(void* address, size_t size, bool is_executable) {
156 UNIMPLEMENTED();
157 }
158
159 #endif
160
161
162 void OS::Sleep(int milliseconds) {
163 unsigned int ms = static_cast<unsigned int>(milliseconds);
164 usleep(1000 * ms);
165 }
166
167
168 void OS::Abort() {
169 // Redirect to std abort to signal abnormal program termination.
170 abort();
171 }
172
173
174 void OS::DebugBreak() {
175 #if defined(__arm__) || defined(__thumb__)
176 asm("bkpt 0");
177 #else
178 asm("int $3");
179 #endif
180 }
181
182
183 class PosixMemoryMappedFile : public OS::MemoryMappedFile {
184 public:
185 PosixMemoryMappedFile(FILE* file, void* memory, int size)
186 : file_(file), memory_(memory), size_(size) { }
187 virtual ~PosixMemoryMappedFile();
188 virtual void* memory() { return memory_; }
189 private:
190 FILE* file_;
191 void* memory_;
192 int size_;
193 };
194
195
196 OS::MemoryMappedFile* OS::MemoryMappedFile::create(const char* name, int size,
197 void* initial) {
198 FILE* file = fopen(name, "w+");
199 if (file == NULL) return NULL;
200 int result = fwrite(initial, size, 1, file);
201 if (result < 1) {
202 fclose(file);
203 return NULL;
204 }
205 void* memory =
206 mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(file), 0);
207 return new PosixMemoryMappedFile(file, memory, size);
208 }
209
210
211 PosixMemoryMappedFile::~PosixMemoryMappedFile() {
212 if (memory_) munmap(memory_, size_);
213 fclose(file_);
214 }
215
216
217 #ifdef ENABLE_LOGGING_AND_PROFILING
218 static unsigned StringToLong(char* buffer) {
219 return static_cast<unsigned>(strtol(buffer, NULL, 16)); // NOLINT
220 }
221 #endif
222
223
224 void OS::LogSharedLibraryAddresses() {
225 #ifdef ENABLE_LOGGING_AND_PROFILING
226 static const int MAP_LENGTH = 1024;
227 int fd = open("/proc/self/maps", O_RDONLY);
228 if (fd < 0) return;
229 while (true) {
230 char addr_buffer[11];
231 addr_buffer[0] = '0';
232 addr_buffer[1] = 'x';
233 addr_buffer[10] = 0;
234 int result = read(fd, addr_buffer + 2, 8);
235 if (result < 8) break;
236 unsigned start = StringToLong(addr_buffer);
237 result = read(fd, addr_buffer + 2, 1);
238 if (result < 1) break;
239 if (addr_buffer[2] != '-') break;
240 result = read(fd, addr_buffer + 2, 8);
241 if (result < 8) break;
242 unsigned end = StringToLong(addr_buffer);
243 char buffer[MAP_LENGTH];
244 int bytes_read = -1;
245 do {
246 bytes_read++;
247 if (bytes_read >= MAP_LENGTH - 1)
248 break;
249 result = read(fd, buffer + bytes_read, 1);
250 if (result < 1) break;
251 } while (buffer[bytes_read] != '\n');
252 buffer[bytes_read] = 0;
253 // Ignore mappings that are not executable.
254 if (buffer[3] != 'x') continue;
255 char* start_of_path = index(buffer, '/');
256 // There may be no filename in this line. Skip to next.
257 if (start_of_path == NULL) continue;
258 buffer[bytes_read] = 0;
259 LOG(SharedLibraryEvent(start_of_path, start, end));
260 }
261 close(fd);
262 #endif
263 }
264
265
266 int OS::StackWalk(Vector<OS::StackFrame> frames) {
267 UNIMPLEMENTED();
268 return 1;
269 }
270
271
272 // Constants used for mmap.
273 static const int kMmapFd = -1;
274 static const int kMmapFdOffset = 0;
275
276
277 VirtualMemory::VirtualMemory(size_t size) {
278 address_ = mmap(NULL, size, PROT_NONE,
279 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
280 kMmapFd, kMmapFdOffset);
281 size_ = size;
282 }
283
284
285 VirtualMemory::~VirtualMemory() {
286 if (IsReserved()) {
287 if (0 == munmap(address(), size())) address_ = MAP_FAILED;
288 }
289 }
290
291
292 bool VirtualMemory::IsReserved() {
293 return address_ != MAP_FAILED;
294 }
295
296
297 bool VirtualMemory::Commit(void* address, size_t size, bool executable) {
298 int prot = PROT_READ | PROT_WRITE | (executable ? PROT_EXEC : 0);
299 if (MAP_FAILED == mmap(address, size, prot,
300 MAP_PRIVATE | MAP_ANON | MAP_FIXED,
301 kMmapFd, kMmapFdOffset)) {
302 return false;
303 }
304
305 UpdateAllocatedSpaceLimits(address, size);
306 return true;
307 }
308
309
310 bool VirtualMemory::Uncommit(void* address, size_t size) {
311 return mmap(address, size, PROT_NONE,
312 MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
313 kMmapFd, kMmapFdOffset) != MAP_FAILED;
314 }
315
316
317 class ThreadHandle::PlatformData : public Malloced {
318 public:
319 explicit PlatformData(ThreadHandle::Kind kind) {
320 Initialize(kind);
321 }
322
323 void Initialize(ThreadHandle::Kind kind) {
324 switch (kind) {
325 case ThreadHandle::SELF: thread_ = pthread_self(); break;
326 case ThreadHandle::INVALID: thread_ = kNoThread; break;
327 }
328 }
329 pthread_t thread_; // Thread handle for pthread.
330 };
331
332
333 ThreadHandle::ThreadHandle(Kind kind) {
334 data_ = new PlatformData(kind);
335 }
336
337
338 void ThreadHandle::Initialize(ThreadHandle::Kind kind) {
339 data_->Initialize(kind);
340 }
341
342
343 ThreadHandle::~ThreadHandle() {
344 delete data_;
345 }
346
347
348 bool ThreadHandle::IsSelf() const {
349 return pthread_equal(data_->thread_, pthread_self());
350 }
351
352
353 bool ThreadHandle::IsValid() const {
354 return data_->thread_ != kNoThread;
355 }
356
357
358 Thread::Thread() : ThreadHandle(ThreadHandle::INVALID) {
359 }
360
361
362 Thread::~Thread() {
363 }
364
365
366 static void* ThreadEntry(void* arg) {
367 Thread* thread = reinterpret_cast<Thread*>(arg);
368 // This is also initialized by the first argument to pthread_create() but we
369 // don't know which thread will run first (the original thread or the new
370 // one) so we initialize it here too.
371 thread->thread_handle_data()->thread_ = pthread_self();
372 ASSERT(thread->IsValid());
373 thread->Run();
374 return NULL;
375 }
376
377
378 void Thread::Start() {
379 pthread_create(&thread_handle_data()->thread_, NULL, ThreadEntry, this);
380 ASSERT(IsValid());
381 }
382
383
384 void Thread::Join() {
385 pthread_join(thread_handle_data()->thread_, NULL);
386 }
387
388
389 Thread::LocalStorageKey Thread::CreateThreadLocalKey() {
390 pthread_key_t key;
391 int result = pthread_key_create(&key, NULL);
392 USE(result);
393 ASSERT(result == 0);
394 return static_cast<LocalStorageKey>(key);
395 }
396
397
398 void Thread::DeleteThreadLocalKey(LocalStorageKey key) {
399 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
400 int result = pthread_key_delete(pthread_key);
401 USE(result);
402 ASSERT(result == 0);
403 }
404
405
406 void* Thread::GetThreadLocal(LocalStorageKey key) {
407 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
408 return pthread_getspecific(pthread_key);
409 }
410
411
412 void Thread::SetThreadLocal(LocalStorageKey key, void* value) {
413 pthread_key_t pthread_key = static_cast<pthread_key_t>(key);
414 pthread_setspecific(pthread_key, value);
415 }
416
417
418 void Thread::YieldCPU() {
419 sched_yield();
420 }
421
422
423 class OpenBSDMutex : public Mutex {
424 public:
425
426 OpenBSDMutex() {
427 pthread_mutexattr_t attrs;
428 int result = pthread_mutexattr_init(&attrs);
429 ASSERT(result == 0);
430 result = pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_RECURSIVE);
431 ASSERT(result == 0);
432 result = pthread_mutex_init(&mutex_, &attrs);
433 ASSERT(result == 0);
434 }
435
436 virtual ~OpenBSDMutex() { pthread_mutex_destroy(&mutex_); }
437
438 virtual int Lock() {
439 int result = pthread_mutex_lock(&mutex_);
440 return result;
441 }
442
443 virtual int Unlock() {
444 int result = pthread_mutex_unlock(&mutex_);
445 return result;
446 }
447
448 private:
449 pthread_mutex_t mutex_; // Pthread mutex for POSIX platforms.
450 };
451
452
453 Mutex* OS::CreateMutex() {
454 return new OpenBSDMutex();
455 }
456
457
458 class OpenBSDSemaphore : public Semaphore {
459 public:
460 explicit OpenBSDSemaphore(int count) { sem_init(&sem_, 0, count); }
461 virtual ~OpenBSDSemaphore() { sem_destroy(&sem_); }
462
463 virtual void Wait();
464 virtual bool Wait(int timeout);
465 virtual void Signal() { sem_post(&sem_); }
466 private:
467 sem_t sem_;
468 };
469
470
471 void OpenBSDSemaphore::Wait() {
472 while (true) {
473 int result = sem_wait(&sem_);
474 if (result == 0) return; // Successfully got semaphore.
475 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
476 }
477 }
478
479
480 bool OpenBSDSemaphore::Wait(int timeout) {
481 const long kOneSecondMicros = 1000000; // NOLINT
482
483 // Split timeout into second and nanosecond parts.
484 struct timeval delta;
485 delta.tv_usec = timeout % kOneSecondMicros;
486 delta.tv_sec = timeout / kOneSecondMicros;
487
488 struct timeval current_time;
489 // Get the current time.
490 if (gettimeofday(&current_time, NULL) == -1) {
491 return false;
492 }
493
494 // Calculate time for end of timeout.
495 struct timeval end_time;
496 timeradd(&current_time, &delta, &end_time);
497
498 struct timespec ts;
499 TIMEVAL_TO_TIMESPEC(&end_time, &ts);
500 while (true) {
501 int result = sem_trywait(&sem_);
502 if (result == 0) return true; // Successfully got semaphore.
503 if (result == -1 && errno == ETIMEDOUT) return false; // Timeout.
504 CHECK(result == -1 && errno == EINTR); // Signal caused spurious wakeup.
505 }
506 }
507
508
509 Semaphore* OS::CreateSemaphore(int count) {
510 return new OpenBSDSemaphore(count);
511 }
512
513
514 #ifdef ENABLE_LOGGING_AND_PROFILING
515
516 static Sampler* active_sampler_ = NULL;
517
518 static void ProfilerSignalHandler(int signal, siginfo_t* info, void* context) {
519 USE(info);
520 if (signal != SIGPROF) return;
521 if (active_sampler_ == NULL) return;
522
523 TickSample sample;
524
525 // We always sample the VM state.
526 sample.state = Logger::state();
527
528 active_sampler_->Tick(&sample);
529 }
530
531
532 class Sampler::PlatformData : public Malloced {
533 public:
534 PlatformData() {
535 signal_handler_installed_ = false;
536 }
537
538 bool signal_handler_installed_;
539 struct sigaction old_signal_handler_;
540 struct itimerval old_timer_value_;
541 };
542
543
544 Sampler::Sampler(int interval, bool profiling)
545 : interval_(interval), profiling_(profiling), active_(false) {
546 data_ = new PlatformData();
547 }
548
549
550 Sampler::~Sampler() {
551 delete data_;
552 }
553
554
555 void Sampler::Start() {
556 // There can only be one active sampler at the time on POSIX
557 // platforms.
558 if (active_sampler_ != NULL) return;
559
560 // Request profiling signals.
561 struct sigaction sa;
562 sa.sa_sigaction = ProfilerSignalHandler;
563 sigemptyset(&sa.sa_mask);
564 sa.sa_flags = SA_SIGINFO;
565 if (sigaction(SIGPROF, &sa, &data_->old_signal_handler_) != 0) return;
566 data_->signal_handler_installed_ = true;
567
568 // Set the itimer to generate a tick for each interval.
569 itimerval itimer;
570 itimer.it_interval.tv_sec = interval_ / 1000;
571 itimer.it_interval.tv_usec = (interval_ % 1000) * 1000;
572 itimer.it_value.tv_sec = itimer.it_interval.tv_sec;
573 itimer.it_value.tv_usec = itimer.it_interval.tv_usec;
574 setitimer(ITIMER_PROF, &itimer, &data_->old_timer_value_);
575
576 // Set this sampler as the active sampler.
577 active_sampler_ = this;
578 active_ = true;
579 }
580
581
582 void Sampler::Stop() {
583 // Restore old signal handler
584 if (data_->signal_handler_installed_) {
585 setitimer(ITIMER_PROF, &data_->old_timer_value_, NULL);
586 sigaction(SIGPROF, &data_->old_signal_handler_, 0);
587 data_->signal_handler_installed_ = false;
588 }
589
590 // This sampler is no longer the active sampler.
591 active_sampler_ = NULL;
592 active_ = false;
593 }
594
595 #endif // ENABLE_LOGGING_AND_PROFILING
596
597 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/dtoa-config.c ('k') | src/platform-posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698