|
| 1 | +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// Getting the stack pointer and getting/setting sp limit. |
| 12 | + |
| 13 | +#ifndef SP_H |
| 14 | +#define SP_H |
| 15 | + |
| 16 | +#include "../../rust_globals.h" |
| 17 | + |
| 18 | +// Gets a pointer to the vicinity of the current stack pointer |
| 19 | +extern "C" ALWAYS_INLINE uintptr_t get_sp() { |
| 20 | + uintptr_t sp; |
| 21 | + asm volatile ( |
| 22 | + "movl %%esp, %0" |
| 23 | + : "=m"(sp)); |
| 24 | + return sp; |
| 25 | +} |
| 26 | + |
| 27 | +// Gets the pointer to the end of the Rust stack from a platform- |
| 28 | +// specific location in the thread control block |
| 29 | +extern "C" CDECL ALWAYS_INLINE uintptr_t get_sp_limit() { |
| 30 | + uintptr_t limit; |
| 31 | + |
| 32 | +#if defined(__linux__) || defined(__FreeBSD__) |
| 33 | + asm volatile ( |
| 34 | + "movl %%gs:48, %0" |
| 35 | + : "=r"(limit)); |
| 36 | +#elif defined(__APPLE__) |
| 37 | + asm volatile ( |
| 38 | + "movl $0x48+90*4, %%ecx\n\t" |
| 39 | + "movl %%gs:(%%ecx), %0" |
| 40 | + : "=r"(limit) |
| 41 | + :: "ecx"); |
| 42 | +#elif defined(_WIN32) |
| 43 | + asm volatile ( |
| 44 | + "movl %%fs:0x14, %0" |
| 45 | + : "=r"(limit)); |
| 46 | +#endif |
| 47 | + |
| 48 | + return limit; |
| 49 | +} |
| 50 | + |
| 51 | +// Records the pointer to the end of the Rust stack in a platform- |
| 52 | +// specific location in the thread control block |
| 53 | +extern "C" CDECL ALWAYS_INLINE void record_sp_limit(void *limit) { |
| 54 | +#if defined(__linux__) || defined(__FreeBSD__) |
| 55 | + asm volatile ( |
| 56 | + "movl %0, %%gs:48" |
| 57 | + :: "r"(limit)); |
| 58 | +#elif defined(__APPLE__) |
| 59 | + asm volatile ( |
| 60 | + "movl $0x48+90*4, %%eax\n\t" |
| 61 | + "movl %0, %%gs:(%%eax)" |
| 62 | + :: "r"(limit) |
| 63 | + : "eax"); |
| 64 | +#elif defined(_WIN32) |
| 65 | + asm volatile ( |
| 66 | + "movl %0, %%fs:0x14" |
| 67 | + :: "r"(limit)); |
| 68 | +#endif |
| 69 | +} |
| 70 | + |
| 71 | +#endif |
0 commit comments