blob: 7aa4f517f6f5f732c3514a105b6f143e1a8693de [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.
Ian Rogers9074b992011-10-26 17:41:55 -070085 {
86 LogMessage log(file, line, ERROR, -1);
87 log.stream() << "Runtime aborting..." << std::endl;
88 // Add Java stack trace if possible
89 Thread* thread = Thread::Current();
90 if (thread != NULL) {
91 log.stream() << "Java stack trace of aborting thread:" << std::endl;
92 thread->DumpStack(log.stream());
93 if (thread->IsExceptionPending()) {
94 Throwable* e = thread->GetException();
95 log.stream() << "Pending exception on thread: " << PrettyTypeOf(e) << std::endl;
96 log.stream() << e->Dump();
97 }
98 }
99 }
Elliott Hughesffe67362011-07-17 12:09:27 -0700100
Elliott Hughesffe67362011-07-17 12:09:27 -0700101 // Perform any platform-specific pre-abort actions.
102 PlatformAbort(file, line);
103
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700104 // use abort hook if we have one
105 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
106 Runtime::Current()->abort_();
107 // notreached
108 }
109
Elliott Hughesffe67362011-07-17 12:09:27 -0700110 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -0700111 // receive SIGABRT. debuggerd dumps the stack trace of the main
112 // thread, whether or not that was the thread that failed. By
113 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -0700114 // fault in the current thread, and get a useful log from debuggerd.
115 // We can also trivially tell the difference between a VM crash and
116 // a deliberate abort by looking at the fault address.
117 *reinterpret_cast<char*>(0xdeadd00d) = 38;
118 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700119 // notreached
120}
121
Elliott Hughesbf86d042011-08-31 17:53:14 -0700122void Runtime::CallExitHook(jint status) {
123 if (exit_ != NULL) {
124 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
125 exit_(status);
126 LOG(WARNING) << "Exit hook returned instead of exiting!";
127 }
128}
129
Brian Carlstrom8a436592011-08-15 21:27:23 -0700130// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
131// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
132// [gG] gigabytes.
133//
134// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700135// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
136// of 1024.
137//
138// The spec says the -Xmx and -Xms options must be multiples of 1024. It
139// doesn't say anything about -Xss.
140//
141// Returns 0 (a useless size) if "s" is malformed or specifies a low or
142// non-evenly-divisible value.
143//
144size_t ParseMemoryOption(const char *s, size_t div) {
145 // strtoul accepts a leading [+-], which we don't want,
146 // so make sure our string starts with a decimal digit.
147 if (isdigit(*s)) {
148 const char *s2;
149 size_t val = strtoul(s, (char **)&s2, 10);
150 if (s2 != s) {
151 // s2 should be pointing just after the number.
152 // If this is the end of the string, the user
153 // has specified a number of bytes. Otherwise,
154 // there should be exactly one more character
155 // that specifies a multiplier.
156 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700157 // The remainder of the string is either a single multiplier
158 // character, or nothing to indicate that the value is in
159 // bytes.
160 char c = *s2++;
161 if (*s2 == '\0') {
162 size_t mul;
163 if (c == '\0') {
164 mul = 1;
165 } else if (c == 'k' || c == 'K') {
166 mul = KB;
167 } else if (c == 'm' || c == 'M') {
168 mul = MB;
169 } else if (c == 'g' || c == 'G') {
170 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700171 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700172 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700173 return 0;
174 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700175
176 if (val <= std::numeric_limits<size_t>::max() / mul) {
177 val *= mul;
178 } else {
179 // Clamp to a multiple of 1024.
180 val = std::numeric_limits<size_t>::max() & ~(1024-1);
181 }
182 } else {
183 // There's more than one character after the numeric part.
184 return 0;
185 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700186 }
187 // The man page says that a -Xm value must be a multiple of 1024.
188 if (val % div == 0) {
189 return val;
190 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700191 }
192 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700193 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700194}
195
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700196size_t ParseIntegerOrDie(const StringPiece& s) {
197 StringPiece::size_type colon = s.find(':');
198 if (colon == StringPiece::npos) {
199 LOG(FATAL) << "Missing integer: " << s;
200 }
201 const char* begin = &s.data()[colon + 1];
202 char* end;
203 size_t result = strtoul(begin, &end, 10);
204 if (begin == end || *end != '\0') {
205 LOG(FATAL) << "Failed to parse integer in: " << s;
206 }
207 return result;
208}
209
Elliott Hughes0af55432011-08-17 18:37:28 -0700210void LoadJniLibrary(JavaVMExt* vm, const char* name) {
211 // TODO: OS_SHARED_LIB_FORMAT_STR
212 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700213 std::string reason;
214 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700215 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
216 << reason;
217 }
218}
219
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700220Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700221 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700222 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700223 const char* boot_class_path = getenv("BOOTCLASSPATH");
224 if (boot_class_path != NULL) {
225 parsed->boot_class_path_ = getenv("BOOTCLASSPATH");
226 }
227 const char* class_path = getenv("CLASSPATH");
228 if (class_path != NULL) {
229 parsed->class_path_ = getenv("CLASSPATH");
230 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700231#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700232 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700233 parsed->check_jni_ = false;
234#else
235 // ...but on by default in debug builds.
236 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700237#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700238
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700239 parsed->heap_initial_size_ = Heap::kInitialSize;
240 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700241 parsed->stack_size_ = Thread::kDefaultStackSize;
242
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700243 parsed->is_zygote_ = false;
244
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700245 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700246 parsed->lock_profiling_threshold_ = 0;
247 parsed->hook_is_sensitive_thread_ = NULL;
248
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700249 parsed->hook_vfprintf_ = vfprintf;
250 parsed->hook_exit_ = exit;
251 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700252
253 for (size_t i = 0; i < options.size(); ++i) {
254 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700255 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700256 LOG(INFO) << "option[" << i << "]=" << option;
257 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700258 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700259 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700260 } else if (option == "-classpath" || option == "-cp") {
261 // TODO: support -Djava.class.path
262 i++;
263 if (i == options.size()) {
264 // TODO: usage
265 LOG(FATAL) << "Missing required class path value for " << option;
266 return NULL;
267 }
268 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700269 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700270 } else if (option.starts_with("-Ximage:")) {
271 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700272 } else if (option.starts_with("-Xcheck:jni")) {
273 parsed->check_jni_ = true;
Elliott Hughes3bb81562011-10-21 18:52:59 -0700274 } else if (option.starts_with("-Xrunjdwp:") || option.starts_with("-agentlib:jdwp=")) {
275 std::string tail = option.substr(option[1] == 'X' ? 10 : 15).ToString();
276 if (tail == "help" || !Dbg::ParseJdwpOptions(tail)) {
277 LOG(FATAL) << "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
278 << "Example: -Xrunjdwp:transport=dt_socket,address=localhost:6500,server=n";
279 return NULL;
280 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700281 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700282 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
283 if (size == 0) {
284 if (ignore_unrecognized) {
285 continue;
286 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700287 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700288 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700289 return NULL;
290 }
291 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700292 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700293 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
294 if (size == 0) {
295 if (ignore_unrecognized) {
296 continue;
297 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700298 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700299 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700300 return NULL;
301 }
302 parsed->heap_maximum_size_ = size;
303 } else if (option.starts_with("-Xss")) {
304 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
305 if (size == 0) {
306 if (ignore_unrecognized) {
307 continue;
308 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700309 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700310 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700311 return NULL;
312 }
313 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700314 } else if (option.starts_with("-D")) {
315 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700316 } else if (option.starts_with("-Xjnitrace:")) {
317 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700318 } else if (option == "compiler") {
319 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700320 } else if (option == "-Xzygote") {
321 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700322 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700323 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700324 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700325 for (size_t i = 0; i < verbose_options.size(); ++i) {
326 parsed->verbose_.insert(verbose_options[i]);
327 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700328 } else if (option.starts_with("-Xjnigreflimit:")) {
329 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700330 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700331 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700332 } else if (option.starts_with("-Xstacktracefile:")) {
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700333// always show stack traces in debug builds
334#ifdef NDEBUG
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700335 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Brian Carlstrom7c6deaa2011-10-21 12:05:06 -0700336#endif
Elliott Hughesfc861622011-10-17 17:57:47 -0700337 } else if (option == "sensitiveThread") {
338 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700339 } else if (option == "vfprintf") {
340 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
341 } else if (option == "exit") {
342 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
343 } else if (option == "abort") {
344 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700345 } else if (option == "host-prefix") {
346 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700347 } else {
348 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700349 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700350 LOG(ERROR) << "Unrecognized option " << option;
351 // TODO: this should exit, but for now tolerate unknown options
352 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700353 }
354 }
355 }
356
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700357 if (!compiler && parsed->images_.empty()) {
358 parsed->images_.push_back("/data/art-cache/boot.art");
359 }
360
Elliott Hughes85d15452011-09-16 17:33:01 -0700361 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
362
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700363 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700364}
365
366Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700367 // TODO: acquire a static mutex on Runtime to avoid racing.
368 if (Runtime::instance_ != NULL) {
369 return NULL;
370 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700371 instance_ = new Runtime;
372 if (!instance_->Init(options, ignore_unrecognized)) {
373 delete instance_;
374 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700375 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700376 return instance_;
377}
Elliott Hughes0af55432011-08-17 18:37:28 -0700378
Brian Carlstromaded5f72011-10-07 17:15:04 -0700379void CreateSystemClassLoader() {
380 if (ClassLoader::UseCompileTimeClassPath()) {
381 return;
382 }
383
384 Thread* self = Thread::Current();
385
386 // Must be in the kNative state for calling native methods.
387 CHECK_EQ(self->GetState(), Thread::kNative);
388
389 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700390 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
391 CHECK(ClassLoader_class.get() != NULL);
392 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
393 "getSystemClassLoader",
394 "()Ljava/lang/ClassLoader;");
395 CHECK(getSystemClassLoader != NULL);
396 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
397 getSystemClassLoader));
398 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700399
Brian Carlstromdf143242011-10-10 18:05:34 -0700400 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700401}
402
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700403void Runtime::Start() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700404 if (IsVerboseStartup()) {
405 LOG(INFO) << "Runtime::Start entering";
406 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700407
408 CHECK(host_prefix_.empty()) << host_prefix_;
409
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700410 // Restore main thread state to kNative as expected by native code
411 Thread::Current()->SetState(Thread::kNative);
412
Elliott Hughes038a8062011-09-18 14:12:41 -0700413 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700414
Elliott Hughes038a8062011-09-18 14:12:41 -0700415 Thread::FinishStartup();
Elliott Hughes85d15452011-09-16 17:33:01 -0700416
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700417 class_linker_->RunRootClinits();
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700418
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700419 // Class::AllocObject asserts that all objects allocated better be
420 // initialized after Runtime::IsStarted is true, so this needs to
421 // come after ClassLinker::RunRootClinits.
422 started_ = true;
423
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700424 if (!is_zygote_) {
425 DidForkFromZygote();
426 }
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700427
Elliott Hughes85d15452011-09-16 17:33:01 -0700428 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700429
Brian Carlstromaded5f72011-10-07 17:15:04 -0700430 CreateSystemClassLoader();
431
Elliott Hughes726079d2011-10-07 18:43:44 -0700432 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
433
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700434 if (IsVerboseStartup()) {
435 LOG(INFO) << "Runtime::Start exiting";
436 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700437}
438
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700439void Runtime::DidForkFromZygote() {
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700440 is_zygote_ = false;
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700441
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700442 StartSignalCatcher();
Elliott Hughesd1cc8362011-10-24 16:58:50 -0700443
444 // Start the JDWP thread. If the command-line debugger flags specified "suspend=y",
445 // this will pause the runtime, so we probably want this to come last.
446 Dbg::StartJdwp();
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700447}
448
449void Runtime::StartSignalCatcher() {
450 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700451 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700452 }
453}
454
Elliott Hughes85d15452011-09-16 17:33:01 -0700455void Runtime::StartDaemonThreads() {
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700456 if (IsVerboseStartup()) {
457 LOG(INFO) << "Runtime::StartDaemonThreads entering";
458 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700459
Elliott Hughes719b3232011-09-25 17:42:19 -0700460 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700461
462 // Must be in the kNative state for calling native methods.
463 CHECK_EQ(self->GetState(), Thread::kNative);
464
Elliott Hughes719b3232011-09-25 17:42:19 -0700465 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700466 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
467 CHECK(c.get() != NULL);
468 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700469 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700470 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700471
472 if (IsVerboseStartup()) {
473 LOG(INFO) << "Runtime::StartDaemonThreads exiting";
474 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700475}
476
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700477bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700478 return started_;
479}
480
Elliott Hughes0af55432011-08-17 18:37:28 -0700481bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700482 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700483
Elliott Hughes90a33692011-08-30 13:27:07 -0700484 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
485 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700486 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700487 return false;
488 }
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700489 verbose_startup_ = options->IsVerbose("startup");
490 if (IsVerboseStartup()) {
491 LOG(INFO) << "Runtime::Init -verbose:startup enabled";
492 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700493
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700494 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughesfc861622011-10-17 17:57:47 -0700495 Monitor::Init(options->IsVerbose("monitor"), options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700496
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700497 host_prefix_ = options->host_prefix_;
498 boot_class_path_ = options->boot_class_path_;
499 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700500 properties_ = options->properties_;
501
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700502 is_zygote_ = options->is_zygote_;
503
Elliott Hughes0af55432011-08-17 18:37:28 -0700504 vfprintf_ = options->hook_vfprintf_;
505 exit_ = options->hook_exit_;
506 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700507
Elliott Hughesbe759c62011-09-08 19:38:21 -0700508 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700509 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700510
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700511 monitor_list_ = new MonitorList;
Elliott Hughes14357e82011-09-26 10:42:15 -0700512 thread_list_ = new ThreadList(options->IsVerbose("thread"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700513 intern_table_ = new InternTable;
514
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700515 Heap::Init(options->IsVerbose("heap"),
516 options->IsVerbose("gc"),
517 options->heap_initial_size_,
518 options->heap_maximum_size_,
519 options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700520
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700521 BlockSignals();
522
Elliott Hughesa0957642011-09-02 14:27:33 -0700523 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700524
Elliott Hughesbe759c62011-09-08 19:38:21 -0700525 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700526
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700527 // ClassLinker needs an attached thread, but we can't fully attach a thread
528 // without creating objects.
529 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700530
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700531 // Set us to runnable so tools using a runtime can allocate and GC by default
532 Thread::Current()->SetState(Thread::kRunnable);
533
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700534 CHECK_GE(Heap::GetSpaces().size(), 1U);
535 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
536 ? ClassLinker::Create(intern_table_)
537 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700538
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700539 if (IsVerboseStartup()) {
540 LOG(INFO) << "Runtime::Init exiting";
541 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700542 return true;
543}
544
Elliott Hughes038a8062011-09-18 14:12:41 -0700545void Runtime::InitNativeMethods() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700546 if (IsVerboseStartup()) {
547 LOG(INFO) << "Runtime::InitNativeMethods entering";
548 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700549 Thread* self = Thread::Current();
550 JNIEnv* env = self->GetJniEnv();
551
Elliott Hughes418d20f2011-09-22 14:00:39 -0700552 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700553 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700554
Elliott Hughes418d20f2011-09-22 14:00:39 -0700555 // First set up JniConstants, which is used by both the runtime's built-in native
556 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700557 JniConstants::init(env);
558
Elliott Hughes418d20f2011-09-22 14:00:39 -0700559 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700560 RegisterRuntimeNativeMethods(env);
561
Elliott Hughes418d20f2011-09-22 14:00:39 -0700562 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
563 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
564 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700565 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700566 if (IsVerboseStartup()) {
567 LOG(INFO) << "Runtime::InitNativeMethods exiting";
568 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700569}
570
571void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
572#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700573 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700574 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700575 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700576 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700577 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700578 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700579 REGISTER(register_java_lang_Object);
580 REGISTER(register_java_lang_Runtime);
581 REGISTER(register_java_lang_String);
582 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700583 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700584 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700585 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700586 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700587 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700588 REGISTER(register_java_lang_reflect_Field);
589 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400590 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700591 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700592 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700593 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700594 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700595#undef REGISTER
596}
597
Elliott Hughes8daa0922011-09-11 13:46:25 -0700598void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700599 // TODO: dump other runtime statistics?
600 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700601 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700602 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
603 // gDvm.numDeclaredMethods,
604 // gDvm.numDeclaredInstFields,
605 // gDvm.numDeclaredStaticFields,
606 // gDvm.pBootLoaderAlloc->curOffset);
607 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700608 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700609
610 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700611}
612
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700613void Runtime::SetStatsEnabled(bool new_state) {
614 if (new_state == true) {
615 GetStats()->Clear(~0);
616 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
617 Thread::Current()->GetStats()->Clear(~0);
618 }
619 stats_enabled_ = new_state;
620}
621
622void Runtime::ResetStats(int kinds) {
623 GetStats()->Clear(kinds & 0xffff);
624 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
625 Thread::Current()->GetStats()->Clear(kinds >> 16);
626}
627
628RuntimeStats* Runtime::GetStats() {
629 return &stats_;
630}
631
632int32_t Runtime::GetStat(int kind) {
633 RuntimeStats* stats;
634 if (kind < (1<<16)) {
635 stats = GetStats();
636 } else {
637 stats = Thread::Current()->GetStats();
638 kind >>= 16;
639 }
640 switch (kind) {
641 case KIND_ALLOCATED_OBJECTS:
642 return stats->allocated_objects;
643 case KIND_ALLOCATED_BYTES:
644 return stats->allocated_bytes;
645 case KIND_FREED_OBJECTS:
646 return stats->freed_objects;
647 case KIND_FREED_BYTES:
648 return stats->freed_bytes;
649 case KIND_GC_INVOCATIONS:
650 return stats->gc_for_alloc_count;
651 case KIND_CLASS_INIT_COUNT:
652 return stats->class_init_count;
653 case KIND_CLASS_INIT_TIME:
654 // Convert ns to us, reduce to 32 bits.
655 return (int) (stats->class_init_time_ns / 1000);
656 case KIND_EXT_ALLOCATED_OBJECTS:
657 case KIND_EXT_ALLOCATED_BYTES:
658 case KIND_EXT_FREED_OBJECTS:
659 case KIND_EXT_FREED_BYTES:
660 return 0; // backward compatibility
661 default:
662 CHECK(false);
663 return -1; // unreachable
664 }
665}
666
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700667void Runtime::BlockSignals() {
668 sigset_t sigset;
669 if (sigemptyset(&sigset) == -1) {
670 PLOG(FATAL) << "sigemptyset failed";
671 }
672 if (sigaddset(&sigset, SIGPIPE) == -1) {
673 PLOG(ERROR) << "sigaddset SIGPIPE failed";
674 }
675 // SIGQUIT is used to dump the runtime's state (including stack traces).
676 if (sigaddset(&sigset, SIGQUIT) == -1) {
677 PLOG(ERROR) << "sigaddset SIGQUIT failed";
678 }
679 // SIGUSR1 is used to initiate a heap dump.
680 if (sigaddset(&sigset, SIGUSR1) == -1) {
681 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
682 }
683 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
684}
685
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700686void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
687 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700688}
689
Elliott Hughesd92bec42011-09-02 17:04:36 -0700690void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700691 // TODO: check we're not calling DetachCurrentThread from a call stack that
692 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700693 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700694}
695
Brian Carlstrome24fa612011-09-29 00:53:55 -0700696void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700697 Dbg::VisitRoots(visitor, arg);
Brian Carlstrome24fa612011-09-29 00:53:55 -0700698 class_linker_->VisitRoots(visitor, arg);
699 intern_table_->VisitRoots(visitor, arg);
700 java_vm_->VisitRoots(visitor, arg);
701 thread_list_->VisitRoots(visitor, arg);
702 visitor(jni_stub_array_, arg);
703 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700704 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
705 visitor(resolution_stub_array_[i], arg);
706 }
707 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
708 visitor(callee_save_method_[i], arg);
709 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700710}
711
712bool Runtime::HasJniStubArray() const {
713 return jni_stub_array_ != NULL;
714}
715
716ByteArray* Runtime::GetJniStubArray() const {
717 CHECK(jni_stub_array_ != NULL);
718 return jni_stub_array_;
719}
720
721void Runtime::SetJniStubArray(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700722 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
723 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
724 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700725 jni_stub_array_ = jni_stub_array;
726}
727
728bool Runtime::HasAbstractMethodErrorStubArray() const {
729 return abstract_method_error_stub_array_ != NULL;
730}
731
732ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
733 CHECK(abstract_method_error_stub_array_ != NULL);
734 return abstract_method_error_stub_array_;
735}
736
737void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
738 CHECK(abstract_method_error_stub_array != NULL);
739 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
740 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
741}
742
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700743
744Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
745 if (method == NULL) {
746 return Runtime::kUnknownMethod;
747 } else if (method->IsStatic()) {
748 return Runtime::kStaticMethod;
749 } else {
750 return Runtime::kInstanceMethod;
751 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700752}
753
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700754bool Runtime::HasResolutionStubArray(TrampolineType type) const {
755 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700756}
757
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700758ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
759 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700760 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700761 return resolution_stub_array_[type];
762}
763
764void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700765 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700766 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
767 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700768}
769
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700770Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700771 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700772 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700773 method->SetDeclaringClass(method_class);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700774 const char* name;
775 if (type == kSaveAll) {
776 name = "$$$callee_save_method$$$";
777 } else if (type == kRefsOnly) {
778 name = "$$$refs_only_callee_save_method$$$";
779 } else {
780 DCHECK(type == kRefsAndArgs);
781 name = "$$$refs_and_args_callee_save_method$$$";
782 }
783 method->SetName(intern_table_->InternStrong(name));
Elliott Hughes30646832011-10-13 16:59:46 -0700784 CHECK(method->GetName() != NULL);
Ian Rogersff1ed472011-09-20 13:46:24 -0700785 method->SetSignature(intern_table_->InternStrong("()V"));
Elliott Hughes30646832011-10-13 16:59:46 -0700786 CHECK(method->GetSignature() != NULL);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700787 method->SetCode(NULL);
Ian Rogersff1ed472011-09-20 13:46:24 -0700788 if ((insns == kThumb2) || (insns == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700789 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
790 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
791 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
792 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
793 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
794 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
795 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
796 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
797 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
798 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
799 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
800 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
801 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
802 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
803 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
804 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
805 (1 << art::arm::S30) | (1 << art::arm::S31);
806 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
807 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
808 __builtin_popcount(fp_spills) /* fprs */ +
809 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700810 method->SetFrameSizeInBytes(frame_size);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700811 method->SetCoreSpillMask(core_spills);
812 method->SetFpSpillMask(fp_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700813 } else if (insns == kX86) {
814 method->SetFrameSizeInBytes(32);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700815 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700816 (1 << art::x86::EDI));
817 method->SetFpSpillMask(0);
818 } else {
819 UNIMPLEMENTED(FATAL);
820 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700821 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700822}
823
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700824bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
825 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700826}
827
Brian Carlstrome24fa612011-09-29 00:53:55 -0700828// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700829Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
830 CHECK(HasCalleeSaveMethod(type));
831 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700832}
833
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700834void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
835 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
836 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700837}
838
Carl Shapiro1fb86202011-06-27 17:43:13 -0700839} // namespace art