| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | #include "master.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  | namespace factor { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-12 23:20:43 -04:00
										 |  |  | callback_heap::callback_heap(cell size, factor_vm* parent) | 
					
						
							|  |  |  |     : seg(new segment(size, true)), here(seg->start), parent(parent) {} | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | callback_heap::~callback_heap() { | 
					
						
							|  |  |  |   delete seg; | 
					
						
							|  |  |  |   seg = NULL; | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  | void factor_vm::init_callbacks(cell size) { | 
					
						
							|  |  |  |   callbacks = new callback_heap(size, this); | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  | bool callback_heap::setup_seh_p() { | 
					
						
							| 
									
										
										
										
											2010-04-04 17:46:36 -04:00
										 |  |  | #if defined(WINDOWS) && defined(FACTOR_X86)
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2010-04-04 17:46:36 -04:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2010-04-04 17:46:36 -04:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  | bool callback_heap::return_takes_param_p() { | 
					
						
							| 
									
										
										
										
											2010-04-04 17:46:36 -04:00
										 |  |  | #if defined(FACTOR_X86) || defined(FACTOR_AMD64)
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2010-04-04 17:46:36 -04:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2010-04-04 17:46:36 -04:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  | instruction_operand callback_heap::callback_operand(code_block* stub, | 
					
						
							|  |  |  |                                                     cell index) { | 
					
						
							|  |  |  |   tagged<array> code_template(parent->special_objects[CALLBACK_STUB]); | 
					
						
							|  |  |  |   tagged<byte_array> relocation_template( | 
					
						
							|  |  |  |       array_nth(code_template.untagged(), 0)); | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   relocation_entry entry(relocation_template->data<relocation_entry>()[index]); | 
					
						
							|  |  |  |   return instruction_operand(entry, stub, 0); | 
					
						
							| 
									
										
										
										
											2010-04-04 17:46:36 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  | void callback_heap::store_callback_operand(code_block* stub, cell index) { | 
					
						
							|  |  |  |   parent->store_external_address(callback_operand(stub, index)); | 
					
						
							| 
									
										
										
										
											2010-04-04 17:46:36 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  | void callback_heap::store_callback_operand(code_block* stub, cell index, | 
					
						
							|  |  |  |                                            cell value) { | 
					
						
							|  |  |  |   callback_operand(stub, index).store_value(value); | 
					
						
							| 
									
										
										
										
											2010-01-10 07:20:32 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-12-02 01:48:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  | void callback_heap::update(code_block* stub) { | 
					
						
							|  |  |  |   store_callback_operand(stub, setup_seh_p() ? 2 : 1, | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |                          (cell)callback_entry_point(stub)); | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   stub->flush_icache(); | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  | code_block* callback_heap::add(cell owner, cell return_rewind) { | 
					
						
							|  |  |  |   tagged<array> code_template(parent->special_objects[CALLBACK_STUB]); | 
					
						
							|  |  |  |   tagged<byte_array> insns(array_nth(code_template.untagged(), 1)); | 
					
						
							|  |  |  |   cell size = array_capacity(insns.untagged()); | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   cell bump = align(size + sizeof(code_block), data_alignment); | 
					
						
							|  |  |  |   if (here + bump > seg->end) | 
					
						
							|  |  |  |     fatal_error("Out of callback space", 0); | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   free_heap_block* free_block = (free_heap_block*)here; | 
					
						
							|  |  |  |   free_block->make_free(bump); | 
					
						
							|  |  |  |   here += bump; | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   code_block* stub = (code_block*)free_block; | 
					
						
							|  |  |  |   stub->owner = owner; | 
					
						
							|  |  |  |   stub->parameters = false_object; | 
					
						
							|  |  |  |   stub->relocation = false_object; | 
					
						
							| 
									
										
										
										
											2009-12-02 01:48:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   memcpy(stub->entry_point(), insns->data<void>(), size); | 
					
						
							| 
									
										
										
										
											2010-01-02 07:03:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   /* Store VM pointer */ | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |   store_callback_operand(stub, 0, (cell)parent); | 
					
						
							| 
									
										
										
										
											2010-04-03 20:24:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   cell index; | 
					
						
							| 
									
										
										
										
											2010-04-04 17:46:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   if (setup_seh_p()) { | 
					
						
							|  |  |  |     store_callback_operand(stub, 1); | 
					
						
							|  |  |  |     index = 1; | 
					
						
							|  |  |  |   } else | 
					
						
							|  |  |  |     index = 0; | 
					
						
							| 
									
										
										
										
											2010-04-03 20:24:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   /* Store VM pointer */ | 
					
						
							|  |  |  |   store_callback_operand(stub, index + 2, (cell) parent); | 
					
						
							| 
									
										
										
										
											2010-01-10 07:20:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   /* On x86, the RET instruction takes an argument which depends on
 | 
					
						
							|  |  |  |      the callback's calling convention */ | 
					
						
							|  |  |  |   if (return_takes_param_p()) | 
					
						
							|  |  |  |     store_callback_operand(stub, index + 3, return_rewind); | 
					
						
							| 
									
										
										
										
											2010-01-02 07:03:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   update(stub); | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   return stub; | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-02 04:31:57 -05:00
										 |  |  | struct callback_updater { | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   callback_heap* callbacks; | 
					
						
							| 
									
										
										
										
											2009-12-02 04:31:57 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-12 23:20:43 -04:00
										 |  |  |   explicit callback_updater(callback_heap* callbacks) | 
					
						
							|  |  |  |       : callbacks(callbacks) {} | 
					
						
							| 
									
										
										
										
											2009-12-02 04:31:57 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   void operator()(code_block* stub) { callbacks->update(stub); } | 
					
						
							| 
									
										
										
										
											2009-12-02 04:31:57 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  | void callback_heap::update() { | 
					
						
							|  |  |  |   callback_updater updater(this); | 
					
						
							|  |  |  |   each_callback(updater); | 
					
						
							| 
									
										
										
										
											2009-12-02 04:31:57 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 01:00:08 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  | void factor_vm::primitive_callback() { | 
					
						
							|  |  |  |   cell return_rewind = to_cell(ctx->pop()); | 
					
						
							|  |  |  |   tagged<word> w(ctx->pop()); | 
					
						
							| 
									
										
										
										
											2010-01-02 07:03:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   w.untag_check(this); | 
					
						
							| 
									
										
										
										
											2011-05-20 18:11:50 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 21:47:11 -04:00
										 |  |  |   void* func = callbacks->add(w.value(), return_rewind)->entry_point(); | 
					
						
							|  |  |  |   CODE_TO_FUNCTION_POINTER_CALLBACK(this, func); | 
					
						
							|  |  |  |   ctx->push(allot_alien(func)); | 
					
						
							| 
									
										
										
										
											2009-10-16 12:39:22 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |