blob: 8672d2963ae0f25fa57f57e23510870fc3045c37 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "runtime.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -07004
Elliott Hughesffe67362011-07-17 12:09:27 -07005#include <cstdio>
6#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -07007#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -07008#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -07009
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "class_linker.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070011#include "class_loader.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070012#include "debugger.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070014#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070015#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070016#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070017#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070018#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070019#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070020#include "signal_catcher.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070021#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070022#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070023#include "thread_list.h"
Elliott Hughes3bb81562011-10-21 18:52:59 -070024#include "UniquePtr.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070025
Elliott Hughes90a33692011-08-30 13:27:07 -070026// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
27#include "JniConstants.h"
28
Carl Shapiro1fb86202011-06-27 17:43:13 -070029namespace art {
30
Carl Shapiro2ed144c2011-07-26 16:52:08 -070031Runtime* Runtime::instance_ = NULL;
32
Elliott Hughesdcc24742011-09-07 14:02:44 -070033Runtime::Runtime()
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070034 : verbose_startup_(false),
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -070035 is_zygote_(false),
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070036 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesc33a32b2011-10-11 18:18:07 -070037 monitor_list_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070038 thread_list_(NULL),
39 intern_table_(NULL),
40 class_linker_(NULL),
41 signal_catcher_(NULL),
42 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070043 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070044 abstract_method_error_stub_array_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070045 started_(false),
46 vfprintf_(NULL),
47 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070048 abort_(NULL),
49 stats_enabled_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070050 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
51 resolution_stub_array_[i] = NULL;
52 }
53 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
54 callee_save_method_[i] = NULL;
55 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070056}
57
Carl Shapiro61e019d2011-07-14 16:53:09 -070058Runtime::~Runtime() {
Elliott Hughesd1cc8362011-10-24 16:58:50 -070059 Dbg::StopJdwp();
60
Elliott Hughes5fe594f2011-09-08 12:33:17 -070061 // Make sure our internal threads are dead before we start tearing down things they're using.
62 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070063 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070064
Elliott Hughes038a8062011-09-18 14:12:41 -070065 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070066 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070067 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070068
Carl Shapiro61e019d2011-07-14 16:53:09 -070069 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070070 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070071 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070072 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070073 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070074 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070075 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070076 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070077}
78
Elliott Hughesffe67362011-07-17 12:09:27 -070079void Runtime::Abort(const char* file, int line) {
80 // Get any pending output out of the way.
81 fflush(NULL);
82
83 // Many people have difficulty distinguish aborts from crashes,
84 // so be explicit.
85 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
86
Elliott Hughesffe67362011-07-17 12:09:27 -070087 // Perform any platform-specific pre-abort actions.
88 PlatformAbort(file, line);
89
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070090 // use abort hook if we have one
91 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
92 Runtime::Current()->abort_();
93 // notreached
94 }
95
Elliott Hughesffe67362011-07-17 12:09:27 -070096 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070097 // receive SIGABRT. debuggerd dumps the stack trace of the main
98 // thread, whether or not that was the thread that failed. By
99 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700100 // fault in the current thread, and get a useful log from debuggerd.
101 // We can also trivially tell the difference between a VM crash and
102 // a deliberate abort by looking at the fault address.
103 *reinterpret_cast<char*>(0xdeadd00d) = 38;
104 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700105 // notreached
106}
107
Elliott Hughesbf86d042011-08-31 17:53:14 -0700108void Runtime::CallExitHook(jint status) {
109 if (exit_ != NULL) {
110 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
111 exit_(status);
112 LOG(WARNING) << "Exit hook returned instead of exiting!";
113 }
114}
115
Brian Carlstrom8a436592011-08-15 21:27:23 -0700116// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
117// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
118// [gG] gigabytes.
119//
120// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700121// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
122// of 1024.
123//
124// The spec says the -Xmx and -Xms options must be multiples of 1024. It
125// doesn't say anything about -Xss.
126//
127// Returns 0 (a useless size) if "s" is malformed or specifies a low or
128// non-evenly-divisible value.
129//
130size_t ParseMemoryOption(const char *s, size_t div) {
131 // strtoul accepts a leading [+-], which we don't want,
132 // so make sure our string starts with a decimal digit.
133 if (isdigit(*s)) {
134 const char *s2;
135 size_t val = strtoul(s, (char **)&s2, 10);
136 if (s2 != s) {
137 // s2 should be pointing just after the number.
138 // If this is the end of the string, the user
139 // has specified a number of bytes. Otherwise,
140 // there should be exactly one more character
141 // that specifies a multiplier.
142 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700143 // The remainder of the string is either a single multiplier
144 // character, or nothing to indicate that the value is in
145 // bytes.
146 char c = *s2++;
147 if (*s2 == '\0') {
148 size_t mul;
149 if (c == '\0') {
150 mul = 1;
151 } else if (c == 'k' || c == 'K') {
152 mul = KB;
153 } else if (c == 'm' || c == 'M') {
154 mul = MB;
155 } else if (c == 'g' || c == 'G') {
156 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700157 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700158 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700159 return 0;
160 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700161
162 if (val <= std::numeric_limits<size_t>::max() / mul) {
163 val *= mul;
164 } else {
165 // Clamp to a multiple of 1024.
166 val = std::numeric_limits<size_t>::max() & ~(1024-1);
167 }
168 } else {
169 // There's more than one character after the numeric part.
170 return 0;
171 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700172 }
173 // The man page says that a -Xm value must be a multiple of 1024.
174 if (val % div == 0) {
175 return val;
176 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700177 }
178 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700179 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700180}
181
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700182size_t ParseIntegerOrDie(const StringPiece& s) {
183 StringPiece::size_type colon = s.find(':');
184 if (colon == StringPiece::npos) {
185 LOG(FATAL) << "Missing integer: " << s;
186 }
187 const char* begin = &s.data()[colon + 1];
188 char* end;
189 size_t result = strtoul(begin, &end, 10);
190 if (begin == end || *end != '\0') {
191 LOG(FATAL) << "Failed to parse integer in: " << s;
192 }
193 return result;
194}
195
Elliott Hughes0af55432011-08-17 18:37:28 -0700196void LoadJniLibrary(JavaVMExt* vm, const char* name) {
197 // TODO: OS_SHARED_LIB_FORMAT_STR
198 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700199 std::string reason;
200 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700201 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
202 << reason;
203 }
204}
205
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700206Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700207 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700208 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700209 const char* boot_class_path = getenv("BOOTCLASSPATH");
210 if (boot_class_path != NULL) {
211 parsed->boot_class_path_ = getenv("BOOTCLASSPATH");
212 }
213 const char* class_path = getenv("CLASSPATH");
214 if (class_path != NULL) {
215 parsed->class_path_ = getenv("CLASSPATH");
216 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700217#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700218 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700219 parsed->check_jni_ = false;
220#else
221 // ...but on by default in debug builds.
222 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700223#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700224
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700225 parsed->heap_initial_size_ = Heap::kInitialSize;
226 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700227 parsed->stack_size_ = Thread::kDefaultStackSize;
228
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700229 parsed->is_zygote_ = false;
230
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700231 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700232 parsed->lock_profiling_threshold_ = 0;
233 parsed->hook_is_sensitive_thread_ = NULL;
234
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700235 parsed->hook_vfprintf_ = vfprintf;
236 parsed->hook_exit_ = exit;
237 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700238
239 for (size_t i = 0; i < options.size(); ++i) {
240 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700241 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700242 LOG(INFO) << "option[" << i << "]=" << option;
243 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700244 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700245 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700246 } else if (option == "-classpath" || option == "-cp") {
247 // TODO: support -Djava.class.path
248 i++;
249 if (i == options.size()) {
250 // TODO: usage
251 LOG(FATAL) << "Missing required class path value for " << option;
252 return NULL;
253 }
254 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700255 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700256 } else if (option.starts_with("-Ximage:")) {
257 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700258 } else if (option.starts_with("-Xcheck:jni")) {
259 parsed->check_jni_ = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700260 } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
261 std::string tail = option.substr(option[1] == 'X' ? 10 : 15).ToString();
262 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
263 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
264 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
265 return NULL;
266 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700267 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700268 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
269 if (size == 0) {
270 if (ignore_unrecognized) {
271 continue;
272 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700273 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700274 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700275 return NULL;
276 }
277 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700278 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700279 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
280 if (size == 0) {
281 if (ignore_unrecognized) {
282 continue;
283 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700284 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700285 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700286 return NULL;
287 }
288 parsed->heap_maximum_size_ = size;
289 } else if (option.starts_with("-Xss")) {
290 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
291 if (size == 0) {
292 if (ignore_unrecognized) {
293 continue;
294 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700295 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700296 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700297 return NULL;
298 }
299 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700300 } else if (option.starts_with("-D")) {
301 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700302 } else if (option.starts_with("-Xjnitrace:")) {
303 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700304 } else if (option == "compiler") {
305 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700306 } else if (option == "-Xzygote") {
307 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700308 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700309 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700310 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700311 for (size_t i = 0; i < verbose_options.size(); ++i) {
312 parsed->verbose_.insert(verbose_options[i]);
313 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700314 } else if (option.starts_with("-Xjnigreflimit:")) {
315 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700316 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700317 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700318 } else if (option.starts_with("-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700319// always show stack traces in debug builds
320#ifdef NDEBUG
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700321 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700322#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700323 } else if (option == "sensitiveThread") {
324 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700325 } else if (option == "vfprintf") {
326 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
327 } else if (option == "exit") {
328 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
329 } else if (option == "abort") {
330 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700331 } else if (option == "host-prefix") {
332 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700333 } else {
334 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700335 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700336 LOG(ERROR) << "Unrecognized option " << option;
337 // TODO: this should exit, but for now tolerate unknown options
338 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700339 }
340 }
341 }
342
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700343 if (!compiler && parsed->images_.empty()) {
344 parsed->images_.push_back("/data/art-cache/boot.art");
345 }
346
Elliott Hughes85d15452011-09-16 17:33:01 -0700347 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
348
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700349 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700350}
351
352Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700353 // TODO: acquire a static mutex on Runtime to avoid racing.
354 if (Runtime::instance_ != NULL) {
355 return NULL;
356 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700357 instance_ = new Runtime;
358 if (!instance_->Init(options, ignore_unrecognized)) {
359 delete instance_;
360 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700361 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700362 return instance_;
363}
Elliott Hughes0af55432011-08-17 18:37:28 -0700364
Brian Carlstromaded5f72011-10-07 17:15:04 -0700365void CreateSystemClassLoader() {
366 if (ClassLoader::UseCompileTimeClassPath()) {
367 return;
368 }
369
370 Thread* self = Thread::Current();
371
372 // Must be in the kNative state for calling native methods.
373 CHECK_EQ(self->GetState(), Thread::kNative);
374
375 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700376 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
377 CHECK(ClassLoader_class.get() != NULL);
378 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
379 "getSystemClassLoader",
380 "()Ljava/lang/ClassLoader;");
381 CHECK(getSystemClassLoader != NULL);
382 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
383 getSystemClassLoader));
384 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700385
Brian Carlstromdf143242011-10-10 18:05:34 -0700386 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700387}
388
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700389void Runtime::Start() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700390 if (IsVerboseStartup()) {
391 LOG(INFO) << "Runtime::Start entering";
392 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700393
394 CHECK(host_prefix_.empty()) << host_prefix_;
395
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700396 // Restore main thread state to kNative as expected by native code
397 Thread::Current()->SetState(Thread::kNative);
398
Elliott Hughes038a8062011-09-18 14:12:41 -0700399 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700400
Elliott Hughes038a8062011-09-18 14:12:41 -0700401 Thread::FinishStartup();
Elliott Hughes85d15452011-09-16 17:33:01 -0700402
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700403 class_linker_->RunRootClinits();
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700404
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700405 // Class::AllocObject asserts that all objects allocated better be
406 // initialized after Runtime::IsStarted is true, so this needs to
407 // come after ClassLinker::RunRootClinits.
408 started_ = true;
409
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700410 if (!is_zygote_) {
411 DidForkFromZygote();
412 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700413
Elliott Hughes85d15452011-09-16 17:33:01 -0700414 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700415
Brian Carlstromaded5f72011-10-07 17:15:04 -0700416 CreateSystemClassLoader();
417
Elliott Hughes726079d2011-10-07 18:43:44 -0700418 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
419
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700420 if (IsVerboseStartup()) {
421 LOG(INFO) << "Runtime::Start exiting";
422 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700423}
424
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700425void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700426 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700427
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700428 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700429
430 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
431 // this will pause the runtime, so we probably want this to come last.
432 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700433}
434
435void Runtime::StartSignalCatcher() {
436 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700437 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700438 }
439}
440
Elliott Hughes85d15452011-09-16 17:33:01 -0700441void Runtime::StartDaemonThreads() {
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700442 if (IsVerboseStartup()) {
443 LOG(INFO) << "Runtime::StartDaemonThreads entering";
444 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700445
Elliott Hughes719b3232011-09-25 17:42:19 -0700446 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700447
448 // Must be in the kNative state for calling native methods.
449 CHECK_EQ(self->GetState(), Thread::kNative);
450
Elliott Hughes719b3232011-09-25 17:42:19 -0700451 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700452 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
453 CHECK(c.get() != NULL);
454 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700455 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700456 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700457
458 if (IsVerboseStartup()) {
459 LOG(INFO) << "Runtime::StartDaemonThreads exiting";
460 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700461}
462
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700463bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700464 return started_;
465}
466
Elliott Hughes0af55432011-08-17 18:37:28 -0700467bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700468 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700469
Elliott Hughes90a33692011-08-30 13:27:07 -0700470 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
471 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700472 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700473 return false;
474 }
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700475 verbose_startup_ = options->IsVerbose("startup");
476 if (IsVerboseStartup()) {
477 LOG(INFO) << "Runtime::Init -verbose:startup enabled";
478 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700479
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700480 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughesfc861622011-10-17 17:57:47 -0700481 Monitor::Init(options->IsVerbose("monitor"), options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700482
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700483 host_prefix_ = options->host_prefix_;
484 boot_class_path_ = options->boot_class_path_;
485 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700486 properties_ = options->properties_;
487
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700488 is_zygote_ = options->is_zygote_;
489
Elliott Hughes0af55432011-08-17 18:37:28 -0700490 vfprintf_ = options->hook_vfprintf_;
491 exit_ = options->hook_exit_;
492 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700493
Elliott Hughesbe759c62011-09-08 19:38:21 -0700494 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700495 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700496
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700497 monitor_list_ = new MonitorList;
Elliott Hughes14357e82011-09-26 10:42:15 -0700498 thread_list_ = new ThreadList(options->IsVerbose("thread"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700499 intern_table_ = new InternTable;
500
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700501 Heap::Init(options->IsVerbose("heap"),
502 options->IsVerbose("gc"),
503 options->heap_initial_size_,
504 options->heap_maximum_size_,
505 options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700506
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700507 BlockSignals();
508
Elliott Hughesa0957642011-09-02 14:27:33 -0700509 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700510
Elliott Hughesbe759c62011-09-08 19:38:21 -0700511 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700512
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700513 // ClassLinker needs an attached thread, but we can't fully attach a thread
514 // without creating objects.
515 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700516
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700517 // Set us to runnable so tools using a runtime can allocate and GC by default
518 Thread::Current()->SetState(Thread::kRunnable);
519
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700520 CHECK_GE(Heap::GetSpaces().size(), 1U);
521 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
522 ? ClassLinker::Create(intern_table_)
523 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700524
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700525 if (IsVerboseStartup()) {
526 LOG(INFO) << "Runtime::Init exiting";
527 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700528 return true;
529}
530
Elliott Hughes038a8062011-09-18 14:12:41 -0700531void Runtime::InitNativeMethods() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700532 if (IsVerboseStartup()) {
533 LOG(INFO) << "Runtime::InitNativeMethods entering";
534 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700535 Thread* self = Thread::Current();
536 JNIEnv* env = self->GetJniEnv();
537
Elliott Hughes418d20f2011-09-22 14:00:39 -0700538 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700539 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700540
Elliott Hughes418d20f2011-09-22 14:00:39 -0700541 // First set up JniConstants, which is used by both the runtime's built-in native
542 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700543 JniConstants::init(env);
544
Elliott Hughes418d20f2011-09-22 14:00:39 -0700545 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700546 RegisterRuntimeNativeMethods(env);
547
Elliott Hughes418d20f2011-09-22 14:00:39 -0700548 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
549 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
550 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700551 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700552 if (IsVerboseStartup()) {
553 LOG(INFO) << "Runtime::InitNativeMethods exiting";
554 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700555}
556
557void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
558#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700559 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700560 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700561 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700562 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700563 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700564 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700565 REGISTER(register_java_lang_Object);
566 REGISTER(register_java_lang_Runtime);
567 REGISTER(register_java_lang_String);
568 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700569 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700570 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700571 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700572 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700573 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700574 REGISTER(register_java_lang_reflect_Field);
575 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400576 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700577 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700578 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700579 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700580 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700581#undef REGISTER
582}
583
Elliott Hughes8daa0922011-09-11 13:46:25 -0700584void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700585 // TODO: dump other runtime statistics?
586 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700587 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700588 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
589 // gDvm.numDeclaredMethods,
590 // gDvm.numDeclaredInstFields,
591 // gDvm.numDeclaredStaticFields,
592 // gDvm.pBootLoaderAlloc->curOffset);
593 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700594 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700595
596 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700597}
598
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700599void Runtime::SetStatsEnabled(bool new_state) {
600 if (new_state == true) {
601 GetStats()->Clear(~0);
602 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
603 Thread::Current()->GetStats()->Clear(~0);
604 }
605 stats_enabled_ = new_state;
606}
607
608void Runtime::ResetStats(int kinds) {
609 GetStats()->Clear(kinds & 0xffff);
610 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
611 Thread::Current()->GetStats()->Clear(kinds >> 16);
612}
613
614RuntimeStats* Runtime::GetStats() {
615 return &stats_;
616}
617
618int32_t Runtime::GetStat(int kind) {
619 RuntimeStats* stats;
620 if (kind < (1<<16)) {
621 stats = GetStats();
622 } else {
623 stats = Thread::Current()->GetStats();
624 kind >>= 16;
625 }
626 switch (kind) {
627 case KIND_ALLOCATED_OBJECTS:
628 return stats->allocated_objects;
629 case KIND_ALLOCATED_BYTES:
630 return stats->allocated_bytes;
631 case KIND_FREED_OBJECTS:
632 return stats->freed_objects;
633 case KIND_FREED_BYTES:
634 return stats->freed_bytes;
635 case KIND_GC_INVOCATIONS:
636 return stats->gc_for_alloc_count;
637 case KIND_CLASS_INIT_COUNT:
638 return stats->class_init_count;
639 case KIND_CLASS_INIT_TIME:
640 // Convert ns to us, reduce to 32 bits.
641 return (int) (stats->class_init_time_ns / 1000);
642 case KIND_EXT_ALLOCATED_OBJECTS:
643 case KIND_EXT_ALLOCATED_BYTES:
644 case KIND_EXT_FREED_OBJECTS:
645 case KIND_EXT_FREED_BYTES:
646 return 0; // backward compatibility
647 default:
648 CHECK(false);
649 return -1; // unreachable
650 }
651}
652
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700653void Runtime::BlockSignals() {
654 sigset_t sigset;
655 if (sigemptyset(&sigset) == -1) {
656 PLOG(FATAL) << "sigemptyset failed";
657 }
658 if (sigaddset(&sigset, SIGPIPE) == -1) {
659 PLOG(ERROR) << "sigaddset SIGPIPE failed";
660 }
661 // SIGQUIT is used to dump the runtime's state (including stack traces).
662 if (sigaddset(&sigset, SIGQUIT) == -1) {
663 PLOG(ERROR) << "sigaddset SIGQUIT failed";
664 }
665 // SIGUSR1 is used to initiate a heap dump.
666 if (sigaddset(&sigset, SIGUSR1) == -1) {
667 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
668 }
669 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
670}
671
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700672void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
673 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700674}
675
Elliott Hughesd92bec42011-09-02 17:04:36 -0700676void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700677 // TODO: check we're not calling DetachCurrentThread from a call stack that
678 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700679 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700680}
681
Brian Carlstrome24fa612011-09-29 00:53:55 -0700682void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
683 class_linker_->VisitRoots(visitor, arg);
684 intern_table_->VisitRoots(visitor, arg);
685 java_vm_->VisitRoots(visitor, arg);
686 thread_list_->VisitRoots(visitor, arg);
687 visitor(jni_stub_array_, arg);
688 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700689 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
690 visitor(resolution_stub_array_[i], arg);
691 }
692 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
693 visitor(callee_save_method_[i], arg);
694 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700695}
696
697bool Runtime::HasJniStubArray() const {
698 return jni_stub_array_ != NULL;
699}
700
701ByteArray* Runtime::GetJniStubArray() const {
702 CHECK(jni_stub_array_ != NULL);
703 return jni_stub_array_;
704}
705
706void Runtime::SetJniStubArray(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700707 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
708 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
709 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700710 jni_stub_array_ = jni_stub_array;
711}
712
713bool Runtime::HasAbstractMethodErrorStubArray() const {
714 return abstract_method_error_stub_array_ != NULL;
715}
716
717ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
718 CHECK(abstract_method_error_stub_array_ != NULL);
719 return abstract_method_error_stub_array_;
720}
721
722void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
723 CHECK(abstract_method_error_stub_array != NULL);
724 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
725 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
726}
727
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700728
729Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
730 if (method == NULL) {
731 return Runtime::kUnknownMethod;
732 } else if (method->IsStatic()) {
733 return Runtime::kStaticMethod;
734 } else {
735 return Runtime::kInstanceMethod;
736 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700737}
738
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700739bool Runtime::HasResolutionStubArray(TrampolineType type) const {
740 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700741}
742
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700743ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
744 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700745 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700746 return resolution_stub_array_[type];
747}
748
749void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700750 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700751 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
752 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700753}
754
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700755Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700756 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700757 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700758 method->SetDeclaringClass(method_class);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700759 const char* name;
760 if (type == kSaveAll) {
761 name = "$$$callee_save_method$$$";
762 } else if (type == kRefsOnly) {
763 name = "$$$refs_only_callee_save_method$$$";
764 } else {
765 DCHECK(type == kRefsAndArgs);
766 name = "$$$refs_and_args_callee_save_method$$$";
767 }
768 method->SetName(intern_table_->InternStrong(name));
Elliott Hughes30646832011-10-13 16:59:46 -0700769 CHECK(method->GetName() != NULL);
Ian Rogersff1ed472011-09-20 13:46:24 -0700770 method->SetSignature(intern_table_->InternStrong("()V"));
Elliott Hughes30646832011-10-13 16:59:46 -0700771 CHECK(method->GetSignature() != NULL);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700772 method->SetCode(NULL);
Ian Rogersff1ed472011-09-20 13:46:24 -0700773 if ((insns == kThumb2) || (insns == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700774 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
775 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
776 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
777 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
778 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
779 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
780 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
781 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
782 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
783 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
784 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
785 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
786 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
787 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
788 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
789 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
790 (1 << art::arm::S30) | (1 << art::arm::S31);
791 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
792 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
793 __builtin_popcount(fp_spills) /* fprs */ +
794 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700795 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700796 method->SetCoreSpillMask(core_spills);
797 method->SetFpSpillMask(fp_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700798 } else if (insns == kX86) {
799 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700800 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700801 (1 << art::x86::EDI));
802 method->SetFpSpillMask(0);
803 } else {
804 UNIMPLEMENTED(FATAL);
805 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700806 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700807}
808
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700809bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
810 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700811}
812
Brian Carlstrome24fa612011-09-29 00:53:55 -0700813// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700814Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
815 CHECK(HasCalleeSaveMethod(type));
816 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700817}
818
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700819void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
820 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
821 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700822}
823
Carl Shapiro1fb86202011-06-27 17:43:13 -0700824} // namespace art