1 // Copyright 2019 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/extensions/cputracemark-extension.h"
6 
7 namespace v8 {
8 namespace internal {
9 
10 v8::Local<v8::FunctionTemplate>
GetNativeFunctionTemplate(v8::Isolate * isolate,v8::Local<v8::String> str)11 CpuTraceMarkExtension::GetNativeFunctionTemplate(v8::Isolate* isolate,
12                                                  v8::Local<v8::String> str) {
13   return v8::FunctionTemplate::New(isolate, CpuTraceMarkExtension::Mark);
14 }
15 
Mark(const v8::FunctionCallbackInfo<v8::Value> & args)16 void CpuTraceMarkExtension::Mark(
17     const v8::FunctionCallbackInfo<v8::Value>& args) {
18   if (args.Length() < 1 || !args[0]->IsUint32()) {
19     args.GetIsolate()->ThrowException(v8::String::NewFromUtf8Literal(
20         args.GetIsolate(),
21         "First parameter to cputracemark() must be a unsigned int32."));
22     return;
23   }
24 
25 #if V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
26 
27 #if defined(__clang__)
28   // for non msvc build
29   uint32_t param =
30       args[0]->Uint32Value(args.GetIsolate()->GetCurrentContext()).ToChecked();
31 
32   int magic_dummy;
33 
34 #if defined(__i386__) && defined(__pic__)
35   __asm__ __volatile__("push %%ebx; cpuid; pop %%ebx"
36                        : "=a"(magic_dummy)
37                        : "a"(0x4711 | ((unsigned)(param) << 16))
38                        : "ecx", "edx");
39 #else
40   __asm__ __volatile__("cpuid"
41                        : "=a"(magic_dummy)
42                        : "a"(0x4711 | ((unsigned)(param) << 16))
43                        : "ecx", "edx", "ebx");
44 #endif  // defined(__i386__) && defined(__pic__)
45 
46 #else
47   // no msvc build support yet.
48 #endif  //! V8_LIBC_MSVCRT
49 
50 #endif  // V8_HOST_ARCH_IA32 || V8_HOST_ARCH_X64
51 }
52 
53 }  // namespace internal
54 }  // namespace v8
55