blob: 64823fd991d4b97652c1ec3e2bb9c07c008665fd [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 Carlstromaded5f72011-10-07 17:15:04 -070010#include "ScopedLocalRef.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070011#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012#include "class_linker.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070013#include "class_loader.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070015#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070016#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070017#include "jni_internal.h"
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070018#include "monitor.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070019#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070020#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070021#include "signal_catcher.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070022#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070023#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070024#include "thread_list.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 Hughes5fe594f2011-09-08 12:33:17 -070059 // Make sure our internal threads are dead before we start tearing down things they're using.
60 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070061 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070062
Elliott Hughes038a8062011-09-18 14:12:41 -070063 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070064 delete thread_list_;
Elliott Hughesc33a32b2011-10-11 18:18:07 -070065 delete monitor_list_;
Elliott Hughes93e74e82011-09-13 11:07:03 -070066
Carl Shapiro61e019d2011-07-14 16:53:09 -070067 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070068 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070069 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070070 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070071 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070072 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070073 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070074 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070075}
76
Elliott Hughesffe67362011-07-17 12:09:27 -070077void Runtime::Abort(const char* file, int line) {
78 // Get any pending output out of the way.
79 fflush(NULL);
80
81 // Many people have difficulty distinguish aborts from crashes,
82 // so be explicit.
83 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
84
Elliott Hughesffe67362011-07-17 12:09:27 -070085 // Perform any platform-specific pre-abort actions.
86 PlatformAbort(file, line);
87
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070088 // use abort hook if we have one
89 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
90 Runtime::Current()->abort_();
91 // notreached
92 }
93
Elliott Hughesffe67362011-07-17 12:09:27 -070094 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070095 // receive SIGABRT. debuggerd dumps the stack trace of the main
96 // thread, whether or not that was the thread that failed. By
97 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070098 // fault in the current thread, and get a useful log from debuggerd.
99 // We can also trivially tell the difference between a VM crash and
100 // a deliberate abort by looking at the fault address.
101 *reinterpret_cast<char*>(0xdeadd00d) = 38;
102 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -0700103 // notreached
104}
105
Elliott Hughesbf86d042011-08-31 17:53:14 -0700106void Runtime::CallExitHook(jint status) {
107 if (exit_ != NULL) {
108 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
109 exit_(status);
110 LOG(WARNING) << "Exit hook returned instead of exiting!";
111 }
112}
113
Brian Carlstrom8a436592011-08-15 21:27:23 -0700114// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
115// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
116// [gG] gigabytes.
117//
118// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700119// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
120// of 1024.
121//
122// The spec says the -Xmx and -Xms options must be multiples of 1024. It
123// doesn't say anything about -Xss.
124//
125// Returns 0 (a useless size) if "s" is malformed or specifies a low or
126// non-evenly-divisible value.
127//
128size_t ParseMemoryOption(const char *s, size_t div) {
129 // strtoul accepts a leading [+-], which we don't want,
130 // so make sure our string starts with a decimal digit.
131 if (isdigit(*s)) {
132 const char *s2;
133 size_t val = strtoul(s, (char **)&s2, 10);
134 if (s2 != s) {
135 // s2 should be pointing just after the number.
136 // If this is the end of the string, the user
137 // has specified a number of bytes. Otherwise,
138 // there should be exactly one more character
139 // that specifies a multiplier.
140 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700141 // The remainder of the string is either a single multiplier
142 // character, or nothing to indicate that the value is in
143 // bytes.
144 char c = *s2++;
145 if (*s2 == '\0') {
146 size_t mul;
147 if (c == '\0') {
148 mul = 1;
149 } else if (c == 'k' || c == 'K') {
150 mul = KB;
151 } else if (c == 'm' || c == 'M') {
152 mul = MB;
153 } else if (c == 'g' || c == 'G') {
154 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700155 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700156 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700157 return 0;
158 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700159
160 if (val <= std::numeric_limits<size_t>::max() / mul) {
161 val *= mul;
162 } else {
163 // Clamp to a multiple of 1024.
164 val = std::numeric_limits<size_t>::max() & ~(1024-1);
165 }
166 } else {
167 // There's more than one character after the numeric part.
168 return 0;
169 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700170 }
171 // The man page says that a -Xm value must be a multiple of 1024.
172 if (val % div == 0) {
173 return val;
174 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700175 }
176 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700177 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700178}
179
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700180size_t ParseIntegerOrDie(const StringPiece& s) {
181 StringPiece::size_type colon = s.find(':');
182 if (colon == StringPiece::npos) {
183 LOG(FATAL) << "Missing integer: " << s;
184 }
185 const char* begin = &s.data()[colon + 1];
186 char* end;
187 size_t result = strtoul(begin, &end, 10);
188 if (begin == end || *end != '\0') {
189 LOG(FATAL) << "Failed to parse integer in: " << s;
190 }
191 return result;
192}
193
Elliott Hughes0af55432011-08-17 18:37:28 -0700194void LoadJniLibrary(JavaVMExt* vm, const char* name) {
195 // TODO: OS_SHARED_LIB_FORMAT_STR
196 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700197 std::string reason;
198 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700199 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
200 << reason;
201 }
202}
203
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700204Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700205 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700206 bool compiler = false;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700207 const char* boot_class_path = getenv("BOOTCLASSPATH");
208 if (boot_class_path != NULL) {
209 parsed->boot_class_path_ = getenv("BOOTCLASSPATH");
210 }
211 const char* class_path = getenv("CLASSPATH");
212 if (class_path != NULL) {
213 parsed->class_path_ = getenv("CLASSPATH");
214 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700215#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700216 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700217 parsed->check_jni_ = false;
218#else
219 // ...but on by default in debug builds.
220 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700221#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700222
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700223 parsed->heap_initial_size_ = Heap::kInitialSize;
224 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700225 parsed->stack_size_ = Thread::kDefaultStackSize;
226
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700227 parsed->is_zygote_ = false;
228
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700229 parsed->jni_globals_max_ = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700230 parsed->lock_profiling_threshold_ = 0;
231 parsed->hook_is_sensitive_thread_ = NULL;
232
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700233 parsed->hook_vfprintf_ = vfprintf;
234 parsed->hook_exit_ = exit;
235 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700236
237 for (size_t i = 0; i < options.size(); ++i) {
238 const StringPiece& option = options[i].first;
Brian Carlstrom0796af02011-10-12 14:31:45 -0700239 if (true && options[0].first == "-Xzygote") {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700240 LOG(INFO) << "option[" << i << "]=" << option;
241 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700242 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700243 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700244 } else if (option == "-classpath" || option == "-cp") {
245 // TODO: support -Djava.class.path
246 i++;
247 if (i == options.size()) {
248 // TODO: usage
249 LOG(FATAL) << "Missing required class path value for " << option;
250 return NULL;
251 }
252 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700253 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700254 } else if (option.starts_with("-Ximage:")) {
255 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700256 } else if (option.starts_with("-Xcheck:jni")) {
257 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700258 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700259 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
260 if (size == 0) {
261 if (ignore_unrecognized) {
262 continue;
263 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700264 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700265 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700266 return NULL;
267 }
268 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700269 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700270 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
271 if (size == 0) {
272 if (ignore_unrecognized) {
273 continue;
274 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700275 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700276 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700277 return NULL;
278 }
279 parsed->heap_maximum_size_ = size;
280 } else if (option.starts_with("-Xss")) {
281 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
282 if (size == 0) {
283 if (ignore_unrecognized) {
284 continue;
285 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700286 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700287 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700288 return NULL;
289 }
290 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700291 } else if (option.starts_with("-D")) {
292 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700293 } else if (option.starts_with("-Xjnitrace:")) {
294 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700295 } else if (option == "compiler") {
296 compiler = true;
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700297 } else if (option == "-Xzygote") {
298 parsed->is_zygote_ = true;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700299 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700300 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700301 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700302 for (size_t i = 0; i < verbose_options.size(); ++i) {
303 parsed->verbose_.insert(verbose_options[i]);
304 }
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700305 } else if (option.starts_with("-Xjnigreflimit:")) {
306 parsed->jni_globals_max_ = ParseIntegerOrDie(option);
Elliott Hughesfc861622011-10-17 17:57:47 -0700307 } else if (option.starts_with("-Xlockprofthreshold:")) {
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700308 parsed->lock_profiling_threshold_ = ParseIntegerOrDie(option);
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700309 } else if (option.starts_with("-Xstacktracefile:")) {
310 parsed->stack_trace_file_ = option.substr(strlen("-Xstacktracefile:")).data();
Elliott Hughesfc861622011-10-17 17:57:47 -0700311 } else if (option == "sensitiveThread") {
312 parsed->hook_is_sensitive_thread_ = reinterpret_cast<bool (*)()>(options[i].second);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700313 } else if (option == "vfprintf") {
314 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
315 } else if (option == "exit") {
316 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
317 } else if (option == "abort") {
318 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700319 } else if (option == "host-prefix") {
320 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700321 } else {
322 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700323 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700324 LOG(ERROR) << "Unrecognized option " << option;
325 // TODO: this should exit, but for now tolerate unknown options
326 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700327 }
328 }
329 }
330
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700331 if (!compiler && parsed->images_.empty()) {
332 parsed->images_.push_back("/data/art-cache/boot.art");
333 }
334
Elliott Hughes85d15452011-09-16 17:33:01 -0700335 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
336
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700337 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700338}
339
340Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700341 // TODO: acquire a static mutex on Runtime to avoid racing.
342 if (Runtime::instance_ != NULL) {
343 return NULL;
344 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700345 instance_ = new Runtime;
346 if (!instance_->Init(options, ignore_unrecognized)) {
347 delete instance_;
348 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700349 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700350 return instance_;
351}
Elliott Hughes0af55432011-08-17 18:37:28 -0700352
Brian Carlstromaded5f72011-10-07 17:15:04 -0700353void CreateSystemClassLoader() {
354 if (ClassLoader::UseCompileTimeClassPath()) {
355 return;
356 }
357
358 Thread* self = Thread::Current();
359
360 // Must be in the kNative state for calling native methods.
361 CHECK_EQ(self->GetState(), Thread::kNative);
362
363 JNIEnv* env = self->GetJniEnv();
Brian Carlstromdf143242011-10-10 18:05:34 -0700364 ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader"));
365 CHECK(ClassLoader_class.get() != NULL);
366 jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(),
367 "getSystemClassLoader",
368 "()Ljava/lang/ClassLoader;");
369 CHECK(getSystemClassLoader != NULL);
370 ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(),
371 getSystemClassLoader));
372 CHECK(class_loader.get() != NULL);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700373
Brian Carlstromdf143242011-10-10 18:05:34 -0700374 Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get()));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700375}
376
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700377void Runtime::Start() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700378 if (IsVerboseStartup()) {
379 LOG(INFO) << "Runtime::Start entering";
380 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700381
382 CHECK(host_prefix_.empty()) << host_prefix_;
383
Elliott Hughes038a8062011-09-18 14:12:41 -0700384 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700385
Elliott Hughes038a8062011-09-18 14:12:41 -0700386 Thread::FinishStartup();
Elliott Hughes85d15452011-09-16 17:33:01 -0700387
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700388 class_linker_->RunRootClinits();
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700389
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700390 // Class::AllocObject asserts that all objects allocated better be
391 // initialized after Runtime::IsStarted is true, so this needs to
392 // come after ClassLinker::RunRootClinits.
393 started_ = true;
394
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700395 StartSignalCatcher();
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700396
Elliott Hughes85d15452011-09-16 17:33:01 -0700397 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700398
Brian Carlstromaded5f72011-10-07 17:15:04 -0700399 CreateSystemClassLoader();
400
Elliott Hughes726079d2011-10-07 18:43:44 -0700401 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
402
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700403 if (IsVerboseStartup()) {
404 LOG(INFO) << "Runtime::Start exiting";
405 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700406}
407
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700408void Runtime::DidForkFromZygote() {
409 CHECK(is_zygote_);
410 is_zygote_ = false;
411 StartSignalCatcher();
412}
413
414void Runtime::StartSignalCatcher() {
415 if (!is_zygote_) {
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700416 signal_catcher_ = new SignalCatcher(stack_trace_file_);
Brian Carlstromcaabb1b2011-10-11 18:09:13 -0700417 }
418}
419
Elliott Hughes85d15452011-09-16 17:33:01 -0700420void Runtime::StartDaemonThreads() {
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700421 if (IsVerboseStartup()) {
422 LOG(INFO) << "Runtime::StartDaemonThreads entering";
423 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700424
Elliott Hughes719b3232011-09-25 17:42:19 -0700425 Thread* self = Thread::Current();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700426
427 // Must be in the kNative state for calling native methods.
428 CHECK_EQ(self->GetState(), Thread::kNative);
429
Elliott Hughes719b3232011-09-25 17:42:19 -0700430 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700431 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
432 CHECK(c.get() != NULL);
433 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700434 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700435 env->CallStaticVoidMethod(c.get(), mid);
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700436
437 if (IsVerboseStartup()) {
438 LOG(INFO) << "Runtime::StartDaemonThreads exiting";
439 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700440}
441
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700442bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700443 return started_;
444}
445
Elliott Hughes0af55432011-08-17 18:37:28 -0700446bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700447 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700448
Elliott Hughes90a33692011-08-30 13:27:07 -0700449 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
450 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700451 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700452 return false;
453 }
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700454 verbose_startup_ = options->IsVerbose("startup");
455 if (IsVerboseStartup()) {
456 LOG(INFO) << "Runtime::Init -verbose:startup enabled";
457 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700458
Elliott Hughesbb1e8f02011-10-18 14:14:25 -0700459 SetJniGlobalsMax(options->jni_globals_max_);
Elliott Hughesfc861622011-10-17 17:57:47 -0700460 Monitor::Init(options->IsVerbose("monitor"), options->lock_profiling_threshold_, options->hook_is_sensitive_thread_);
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700461
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700462 host_prefix_ = options->host_prefix_;
463 boot_class_path_ = options->boot_class_path_;
464 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700465 properties_ = options->properties_;
466
Elliott Hughes9ca7a1f2011-10-11 14:29:52 -0700467 is_zygote_ = options->is_zygote_;
468
Elliott Hughes0af55432011-08-17 18:37:28 -0700469 vfprintf_ = options->hook_vfprintf_;
470 exit_ = options->hook_exit_;
471 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700472
Elliott Hughesbe759c62011-09-08 19:38:21 -0700473 default_stack_size_ = options->stack_size_;
Elliott Hughes94ce37a2011-10-18 15:07:48 -0700474 stack_trace_file_ = options->stack_trace_file_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700475
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700476 monitor_list_ = new MonitorList;
Elliott Hughes14357e82011-09-26 10:42:15 -0700477 thread_list_ = new ThreadList(options->IsVerbose("thread"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700478 intern_table_ = new InternTable;
479
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700480 Heap::Init(options->heap_initial_size_, options->heap_maximum_size_, options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700481
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700482 BlockSignals();
483
Elliott Hughesa0957642011-09-02 14:27:33 -0700484 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700485
Elliott Hughesbe759c62011-09-08 19:38:21 -0700486 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700487
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700488 // ClassLinker needs an attached thread, but we can't fully attach a thread
489 // without creating objects.
490 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700491
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700492 CHECK_GE(Heap::GetSpaces().size(), 1U);
493 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
494 ? ClassLinker::Create(intern_table_)
495 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700496
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700497 if (IsVerboseStartup()) {
498 LOG(INFO) << "Runtime::Init exiting";
499 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700500 return true;
501}
502
Elliott Hughes038a8062011-09-18 14:12:41 -0700503void Runtime::InitNativeMethods() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700504 if (IsVerboseStartup()) {
505 LOG(INFO) << "Runtime::InitNativeMethods entering";
506 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700507 Thread* self = Thread::Current();
508 JNIEnv* env = self->GetJniEnv();
509
Elliott Hughes418d20f2011-09-22 14:00:39 -0700510 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Brian Carlstromaded5f72011-10-07 17:15:04 -0700511 CHECK_EQ(self->GetState(), Thread::kNative);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700512
Elliott Hughes418d20f2011-09-22 14:00:39 -0700513 // First set up JniConstants, which is used by both the runtime's built-in native
514 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700515 JniConstants::init(env);
516
Elliott Hughes418d20f2011-09-22 14:00:39 -0700517 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700518 RegisterRuntimeNativeMethods(env);
519
Elliott Hughes418d20f2011-09-22 14:00:39 -0700520 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
521 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
522 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700523 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700524 if (IsVerboseStartup()) {
525 LOG(INFO) << "Runtime::InitNativeMethods exiting";
526 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700527}
528
529void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
530#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700531 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700532 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700533 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700534 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700535 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700536 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700537 REGISTER(register_java_lang_Object);
538 REGISTER(register_java_lang_Runtime);
539 REGISTER(register_java_lang_String);
540 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700541 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700542 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700543 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700544 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700545 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700546 REGISTER(register_java_lang_reflect_Field);
547 REGISTER(register_java_lang_reflect_Method);
Jesse Wilson95caa792011-10-12 18:14:17 -0400548 REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700549 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700550 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700551 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700552 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700553#undef REGISTER
554}
555
Elliott Hughes8daa0922011-09-11 13:46:25 -0700556void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700557 // TODO: dump other runtime statistics?
558 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700559 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700560 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
561 // gDvm.numDeclaredMethods,
562 // gDvm.numDeclaredInstFields,
563 // gDvm.numDeclaredStaticFields,
564 // gDvm.pBootLoaderAlloc->curOffset);
565 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700566 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700567
568 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700569}
570
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700571void Runtime::SetStatsEnabled(bool new_state) {
572 if (new_state == true) {
573 GetStats()->Clear(~0);
574 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
575 Thread::Current()->GetStats()->Clear(~0);
576 }
577 stats_enabled_ = new_state;
578}
579
580void Runtime::ResetStats(int kinds) {
581 GetStats()->Clear(kinds & 0xffff);
582 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
583 Thread::Current()->GetStats()->Clear(kinds >> 16);
584}
585
586RuntimeStats* Runtime::GetStats() {
587 return &stats_;
588}
589
590int32_t Runtime::GetStat(int kind) {
591 RuntimeStats* stats;
592 if (kind < (1<<16)) {
593 stats = GetStats();
594 } else {
595 stats = Thread::Current()->GetStats();
596 kind >>= 16;
597 }
598 switch (kind) {
599 case KIND_ALLOCATED_OBJECTS:
600 return stats->allocated_objects;
601 case KIND_ALLOCATED_BYTES:
602 return stats->allocated_bytes;
603 case KIND_FREED_OBJECTS:
604 return stats->freed_objects;
605 case KIND_FREED_BYTES:
606 return stats->freed_bytes;
607 case KIND_GC_INVOCATIONS:
608 return stats->gc_for_alloc_count;
609 case KIND_CLASS_INIT_COUNT:
610 return stats->class_init_count;
611 case KIND_CLASS_INIT_TIME:
612 // Convert ns to us, reduce to 32 bits.
613 return (int) (stats->class_init_time_ns / 1000);
614 case KIND_EXT_ALLOCATED_OBJECTS:
615 case KIND_EXT_ALLOCATED_BYTES:
616 case KIND_EXT_FREED_OBJECTS:
617 case KIND_EXT_FREED_BYTES:
618 return 0; // backward compatibility
619 default:
620 CHECK(false);
621 return -1; // unreachable
622 }
623}
624
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700625void Runtime::BlockSignals() {
626 sigset_t sigset;
627 if (sigemptyset(&sigset) == -1) {
628 PLOG(FATAL) << "sigemptyset failed";
629 }
630 if (sigaddset(&sigset, SIGPIPE) == -1) {
631 PLOG(ERROR) << "sigaddset SIGPIPE failed";
632 }
633 // SIGQUIT is used to dump the runtime's state (including stack traces).
634 if (sigaddset(&sigset, SIGQUIT) == -1) {
635 PLOG(ERROR) << "sigaddset SIGQUIT failed";
636 }
637 // SIGUSR1 is used to initiate a heap dump.
638 if (sigaddset(&sigset, SIGUSR1) == -1) {
639 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
640 }
641 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
642}
643
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700644void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
645 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700646}
647
Elliott Hughesd92bec42011-09-02 17:04:36 -0700648void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700649 // TODO: check we're not calling DetachCurrentThread from a call stack that
650 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700651 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700652}
653
Brian Carlstrome24fa612011-09-29 00:53:55 -0700654void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
655 class_linker_->VisitRoots(visitor, arg);
656 intern_table_->VisitRoots(visitor, arg);
657 java_vm_->VisitRoots(visitor, arg);
658 thread_list_->VisitRoots(visitor, arg);
659 visitor(jni_stub_array_, arg);
660 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700661 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
662 visitor(resolution_stub_array_[i], arg);
663 }
664 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
665 visitor(callee_save_method_[i], arg);
666 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700667}
668
669bool Runtime::HasJniStubArray() const {
670 return jni_stub_array_ != NULL;
671}
672
673ByteArray* Runtime::GetJniStubArray() const {
674 CHECK(jni_stub_array_ != NULL);
675 return jni_stub_array_;
676}
677
678void Runtime::SetJniStubArray(ByteArray* jni_stub_array) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700679 CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array;
680 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array)
681 << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700682 jni_stub_array_ = jni_stub_array;
683}
684
685bool Runtime::HasAbstractMethodErrorStubArray() const {
686 return abstract_method_error_stub_array_ != NULL;
687}
688
689ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
690 CHECK(abstract_method_error_stub_array_ != NULL);
691 return abstract_method_error_stub_array_;
692}
693
694void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
695 CHECK(abstract_method_error_stub_array != NULL);
696 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
697 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
698}
699
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700700
701Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
702 if (method == NULL) {
703 return Runtime::kUnknownMethod;
704 } else if (method->IsStatic()) {
705 return Runtime::kStaticMethod;
706 } else {
707 return Runtime::kInstanceMethod;
708 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700709}
710
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700711bool Runtime::HasResolutionStubArray(TrampolineType type) const {
712 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700713}
714
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700715ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
716 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700717 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700718 return resolution_stub_array_[type];
719}
720
721void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700722 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700723 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
724 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700725}
726
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700727Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700728 Class* method_class = Method::GetMethodClass();
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700729 SirtRef<Method> method(down_cast<Method*>(method_class->AllocObject()));
Ian Rogersff1ed472011-09-20 13:46:24 -0700730 method->SetDeclaringClass(method_class);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700731 const char* name;
732 if (type == kSaveAll) {
733 name = "$$$callee_save_method$$$";
734 } else if (type == kRefsOnly) {
735 name = "$$$refs_only_callee_save_method$$$";
736 } else {
737 DCHECK(type == kRefsAndArgs);
738 name = "$$$refs_and_args_callee_save_method$$$";
739 }
740 method->SetName(intern_table_->InternStrong(name));
Elliott Hughes30646832011-10-13 16:59:46 -0700741 CHECK(method->GetName() != NULL);
Ian Rogersff1ed472011-09-20 13:46:24 -0700742 method->SetSignature(intern_table_->InternStrong("()V"));
Elliott Hughes30646832011-10-13 16:59:46 -0700743 CHECK(method->GetSignature() != NULL);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700744 method->SetCode(NULL);
Ian Rogersff1ed472011-09-20 13:46:24 -0700745 if ((insns == kThumb2) || (insns == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700746 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
747 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
748 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
749 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
750 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
751 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
752 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
753 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
754 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
755 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
756 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
757 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
758 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
759 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
760 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
761 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
762 (1 << art::arm::S30) | (1 << art::arm::S31);
763 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
764 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
765 __builtin_popcount(fp_spills) /* fprs */ +
766 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700767 method->SetFrameSizeInBytes(frame_size);
768 method->SetReturnPcOffsetInBytes(frame_size - kPointerSize);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700769 method->SetCoreSpillMask(core_spills);
770 method->SetFpSpillMask(fp_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700771 } else if (insns == kX86) {
772 method->SetFrameSizeInBytes(32);
773 method->SetReturnPcOffsetInBytes(28);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700774 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700775 (1 << art::x86::EDI));
776 method->SetFpSpillMask(0);
777 } else {
778 UNIMPLEMENTED(FATAL);
779 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700780 return method.get();
Ian Rogersff1ed472011-09-20 13:46:24 -0700781}
782
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700783bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
784 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700785}
786
Brian Carlstrome24fa612011-09-29 00:53:55 -0700787// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700788Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
789 CHECK(HasCalleeSaveMethod(type));
790 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700791}
792
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700793void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
794 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
795 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700796}
797
Carl Shapiro1fb86202011-06-27 17:43:13 -0700798} // namespace art