Skip to content

Commit e9758b9

Browse files
Benoît Canetnyh
authored andcommitted
sched/elf: Pave the way for creating new namespace
In order to create a new partial namespace a new instance of elf::program is needed. This patch keep elf::s_program for all running thread while creating a way to add a new per application elf::program when needed. This idea was suggested by Glauber as an alternative to the previous series which was somewhat intrusive. This patch also introduces an application method that allows a new program to be created for the calling thread. The new program is created at a different slice of the address space. This effectively imposes a lower amount of memory each program can use. But the limit is still big enough, and should be enough for anybody (tm). Signed-off-by: Benoît Canet <[email protected]> Signed-off-by: Glauber Costa <[email protected]> Signed-off-by: Nadav Har'El <[email protected]>
1 parent d4cf98f commit e9758b9

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

core/app.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,36 @@ std::string application::get_command()
355355
return _command;
356356
}
357357

358+
// For simplicity, we will not reuse bits in the bitmap, since no destructor is
359+
// assigned to the program. In that case, a simple counter would do. But coding
360+
// this way is easy, and make this future extension simple.
361+
constexpr int max_namespaces = 32;
362+
std::bitset<max_namespaces> namespaces(1);
363+
364+
void application::new_program()
365+
{
366+
unsigned long i = 0;
367+
368+
for (i = 0; i < max_namespaces; ++i) {
369+
if (!namespaces.test(i)) {
370+
namespaces.set(i);
371+
break;
372+
}
373+
}
374+
375+
if (i == max_namespaces) {
376+
return;
377+
}
378+
379+
void *addr = reinterpret_cast<void *>(elf::program_base) + ((i + 1) << 33);
380+
_program.reset(new elf::program(addr));
381+
}
382+
383+
elf::program *application::program() {
384+
return _program.get();
385+
}
386+
387+
358388
namespace this_application {
359389

360390
void on_termination_request(std::function<void()> callback)

core/elf.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include <osv/elf.hh>
9+
#include <osv/app.hh>
910
#include <osv/mmu.hh>
1011
#include <boost/format.hpp>
1112
#include <exception>
@@ -1017,14 +1018,19 @@ void object::init_static_tls()
10171018

10181019
program* s_program;
10191020

1021+
void create_main_program()
1022+
{
1023+
assert(!s_program);
1024+
s_program = new elf::program();
1025+
}
1026+
10201027
program::program(void* addr)
10211028
: _next_alloc(addr)
10221029
{
1023-
assert(!s_program);
1024-
s_program = this;
10251030
_core = make_shared<memory_image>(*this, (void*)ELF_IMAGE_START);
10261031
assert(_core->module_index() == core_module_index);
10271032
_core->load_segments();
1033+
set_search_path({"/", "/usr/lib"});
10281034
// Our kernel already supplies the features of a bunch of traditional
10291035
// shared libraries:
10301036
static const auto supplied_modules = {
@@ -1310,6 +1316,12 @@ object *program::object_containing_addr(const void *addr)
13101316

13111317
program* get_program()
13121318
{
1319+
auto app = osv::application::get_current();
1320+
1321+
if (app && app->program()) {
1322+
return app->program();
1323+
}
1324+
13131325
return s_program;
13141326
}
13151327

include/osv/app.hh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ public:
145145

146146
std::shared_ptr<application_runtime> runtime() const { return _runtime; }
147147
std::shared_ptr<elf::object> lib() const { return _lib; }
148+
149+
elf::program *program();
148150
private:
151+
void new_program();
149152
shared_app_t get_shared() {
150153
return shared_from_this();
151154
}
@@ -160,6 +163,7 @@ private:
160163
using main_func_t = int(int, char**);
161164

162165
pthread_t _thread;
166+
std::unique_ptr<elf::program> _program; // namespace program
163167
std::vector<std::string> _args;
164168
std::string _command;
165169
int _return_code;

include/osv/elf.hh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,10 @@ private:
596596
friend class object;
597597
};
598598

599+
void create_main_program();
600+
599601
/**
600-
* Get the single elf::program instance
602+
* Get the current elf::program instance
601603
*/
602604
program* get_program();
603605

loader.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,8 @@ void main_cont(int ac, char** av)
528528

529529
debug("Firmware vendor: %s\n", osv::firmware_vendor().c_str());
530530

531-
new elf::program();
532-
elf::get_program()->set_search_path({"/", "/usr/lib"});
531+
elf::create_main_program();
532+
533533
std::vector<std::vector<std::string> > cmds;
534534

535535
std::tie(ac, av) = parse_options(ac, av);

0 commit comments

Comments
 (0)